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:
def solution(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].
def solution(A):
max_length = max(max(A),len(A))
matrix = []
for i in A:
matrix.append([ 0 if j>=i else 1 for j in range(max_length)])
answer = [0 for i in range(len(matrix))]
max_square = 0
for j in range(len(matrix[0])):
for i in range(len(matrix)):
if int(matrix[i][j]):
answer[i] += int(matrix[i][j])
else:
answer[i] = 0
if max(answer) >= max_square+1:
max_square = max_square_val(answer,max_square)
return(max_square)
def max_square_val(answer,max_square):
max_square+=1
if max_square > len(answer):
return max_square-1
count = 0
for val in answer:
if val >= max_square:
count+=1
else:
count = 0
if count == max_square:
return(max_square)
return max_square-1
def solution(A):
max_length = max(max(A),len(A))
matrix = []
for i in A:
matrix.append([ 0 if j>=i else 1 for j in range(max_length)])
answer = [0 for i in range(len(matrix))]
max_square = 0
for j in range(len(matrix[0])):
for i in range(len(matrix)):
if int(matrix[i][j]):
answer[i] += int(matrix[i][j])
else:
answer[i] = 0
if max(answer) >= max_square+1:
max_square = max_square_val(answer,max_square)
return(max_square)
def max_square_val(answer,max_square):
max_square+=1
if max_square > len(answer):
return max_square-1
count = 0
for val in answer:
if val >= max_square:
count+=1
else:
count = 0
if count == max_square:
return(max_square)
return max_square-1
def solution(A):
max_length = max(max(A),len(A))
matrix = []
for i in A:
matrix.append([ 0 if j>=i else 1 for j in range(max_length)])
answer = [0 for i in range(len(matrix))]
max_square = 0
for j in range(len(matrix[0])):
for i in range(len(matrix)):
if int(matrix[i][j]):
answer[i] += int(matrix[i][j])
else:
answer[i] = 0
if max(answer) >= max_square+1:
max_square = max_square_val(answer,max_square)
return(max_square)
def max_square_val(answer,max_square):
max_square+=1
if max_square > len(answer):
return max_square-1
count = 0
for val in answer:
if val >= max_square:
count+=1
else:
count = 0
if count == max_square:
return(max_square)
return max_square-1
def solution(A):
max_length = max(max(A),len(A))
matrix = []
for i in A:
matrix.append([ 0 if j>=i else 1 for j in range(max_length)])
answer = [0 for i in range(len(matrix))]
max_square = 0
for j in range(len(matrix[0])):
for i in range(len(matrix)):
if int(matrix[i][j]):
answer[i] += int(matrix[i][j])
else:
answer[i] = 0
if max(answer) >= max_square+1:
max_square = max_square_val(answer,max_square)
return(max_square)
def max_square_val(answer,max_square):
max_square+=1
if max_square > len(answer):
return max_square-1
count = 0
for val in answer:
if val >= max_square:
count+=1
else:
count = 0
if count == max_square:
return(max_square)
return max_square-1
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.
Various combinations of monotonic and constant sequences. N <= 750.
running time: 0.284 sec., time limit: 0.176 sec.
Randomly generated tests. N <= 10,000. Score x 2.
Killed. Hard limit reached: 6.000 sec.
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 6, in solution matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) File "/tmp/solution.py", line 6, in <listcomp> matrix.append([ 0 if j>=i else 1 for j in range(max_length)]) MemoryError