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].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h == w:
result.append(h)
elif h > w:
right += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h == w:
result.append(h)
left += 1
elif h > w:
right += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h == w:
result.append(h)
left += 1
elif h > w:
right += 1
elif h < w:
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h == w:
result.append(h)
left += 1
elif h > w:
right += 1
elif h < w:
left += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h == w:
result.append(h)
left += 1
if h > w:
right += 1
elif h < w:
left += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max()
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max(right, left)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max(right, left)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max(right, left)
print(r)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max(right, left)
print(result)
return max(result)
[1, 2, 1]
[3, 3, 3]
[2, 2]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if h > w:
right += 1
elif h < w:
left += 1
else:
result.append(h)
left += 1
right = max(right, left)
print(result)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
pass
else:
print(result)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
else:
print(result)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
print(result)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
print(result)
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
print(result)
return max(result)
[1, 1, 2, 2, 1, 1]
[1, 2, 3, 3, 3]
[1, 2, 3, 4, 2, 2]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = []
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result =
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result.append(w)
right += 1
else:
left += 1
return max(result)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return max(result)
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 16, in solution return max(result) TypeError: 'int' object is not iterable
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 16, in solution return max(result) TypeError: 'int' object is not iterable
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 16, in solution return max(result) TypeError: 'int' object is not iterable
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return res
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left, right = 0, 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left = right = 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left = right = 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left = right = 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return result
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
result = 0
left = right = 0
while right < len(A):
h = min(A[left:right+1])
w = right-left+1
if w <= h:
result = max(result, w)
right += 1
else:
left += 1
return result
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: 1.552 sec., time limit: 0.256 sec.