You are given an N × N matrix in which every cell is colored black or white. Columns are numbered from 0 to N-1 (from left to right). This coloring is represented by a non-empty array of integers A. If the K-th number in the array is equal to X then the X lowest cells in the K-th column of the matrix are black. The rest of the cells in the K-th column are white. The task is to calculate the side length of the biggest black square (a square containing only black cells).
Write a function:
class Solution { public int solution(int[] A); }
that, given an array of integers A of length N representing the coloring of the matrix, returns the side length of the biggest black square.
Examples:
1. Given A = [1, 2, 5, 3, 1, 3], the function should return 2. For example, the black square of side 2 contains the two lowest rows of the 1st and 2nd columns (counting from 0).
2. Given A = [3, 3, 3, 5, 4], the function should return 3. For example, the biggest black square has side 3 and contains the three lowest rows of the last three columns.
3. Given A = [6, 5, 5, 6, 2, 2], the function should return 4. The biggest black square has side 4 and contains the four lowest rows of the first four columns.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [1..N].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int s=A.length-i+1; // test count
for(int j=0; j<s; j++){
int t=0;
int f=i+1;//square count
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int s=A.length-i+1; // test count
int f=i+1;//square count, no to find
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i+1; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
if
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=t;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f;
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=t) break; //not found
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=t) break; //not found
}
return r;
}
}
Solution.java:29: error: cannot find symbol if(r!=t) break; //not found ^ symbol: variable t location: class Solution 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=(i+1)) break; //not found
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=f) break; //not found
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=f) break; //not found
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=f) break; //not found
}
return r;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int r=0;
for (int i=0; i<A.length; i++){
int f=i+1;//square count, no to find
int s=A.length-i; // test count
for(int j=0; j<s; j++){
int t=0;
for(int k=0; k<f; k++){
if(A[j+k]>=f){
t++;
if(t==f) break;
}else{
break;
}
}
if(t==f) {
r=f; //square found
break;
}
}
if(r!=f) break; //not found
}
return r;
}
}
The following issues have been detected: timeout errors.
Tests with the biggest square surrounded by shorter columns. N <= 10.
Tests with alternating small and big numbers (relatively). N <= 15.
Two monotonic sequences or one monotonic and one constant. N <= 75.
Tests with local maximums and local minimums. N <= 75.
Randomly generated tests. N <= 10,000. Score x 2.
running time: 0.360 sec., time limit: 0.128 sec.