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].
[10]*10
{1: [0, 4], 2: [1], 5: [2], 3: [3, 5]}
{3: [0, 1, 2], 5: [3], 4: [4]}
{6: [0, 3], 5: [1, 2], 2: [4, 5]}
[10]*10
{1: [0, 4], 2: [1], 5: [2], 3: [3, 5]}
{3: [0, 1, 2], 5: [3], 4: [4]}
{6: [0, 3], 5: [1, 2], 2: [4, 5]}
[10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
print(heights)
print(height_locs)
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
return 1
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]}
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]}
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]}
[10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
for h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
for h in heights:
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
for h in heights:
for idx in
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
for h in heights:
for idx in height_locs[h]:
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
for h in heights:
for idx in height_locs[h]:
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
if idx > 0:
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
if idx > 0:
left
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
if idx > 0:
left = squares[idx-1]
else:
left = 0
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
if idx > 0:
left = squares[idx-1]
left = 0
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = 0
if idx > 0: left = squares[idx-1]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
squares[idx]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
squares[idx - left]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
squares[idx - left] += 1
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] += squares
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >=
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= h:
return squares[idx-left]
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
for h in heights:
for idx in height_locs[h]:
if
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return 1
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]}
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]}
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]}
[10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print()
if biggestSquare >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] += squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 3, 0, 0] [0, 0, 2, 3, 0, 1] [0, 4, 2, 4, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 3] [1, 0, 0, 2, 3] [2, 3, 0, 2, 3] [2, 3, 5, 2, 9]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 3, 0, 1, 0, 0] [2, 3, 2, 3, 0, 3]
[10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
[10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return 1
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 1
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return return
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 2
[10] {10: [0, 1]} [1, 0] [2, 2]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 1
[10, 1] {10: [0, 2], 1: [1]} [1, 0, 0] [1, 0, 1] [3, 2, 3]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[10, 1, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 1
[10, 2, 1] {10: [0, 4], 1: [1, 3], 2: [2]} [1, 0, 0, 0, 0] [1, 0, 0, 0, 1] [1, 0, 1, 0, 1] [3, 2, 3, 0, 1]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[10, 1, 2, 1, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 2
[10, 3, 2, 1] {10: [0, 4], 1: [1], 3: [2], 2: [3]} [1, 0, 0, 0, 0] [1, 0, 0, 0, 1] [1, 0, 1, 0, 1] [1, 0, 3, 2, 3]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[10, 1, 3, 2, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
print(heights)
print(height_locs)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
[5, 3, 2, 1] {1: [0, 4], 2: [1], 5: [2], 3: [3, 5]} [0, 0, 1, 0, 0, 0] [0, 0, 2, 2, 0, 0] [0, 0, 2, 2, 0, 1] [0, 3, 2, 3, 0, 1]
[5, 4, 3] {3: [0, 1, 2], 5: [3], 4: [4]} [0, 0, 0, 1, 0] [0, 0, 0, 2, 2] [1, 0, 0, 2, 2] [2, 2, 0, 2, 2] [5, 2, 3, 2, 5]
[6, 5, 2] {6: [0, 3], 5: [1, 2], 2: [4, 5]} [1, 0, 0, 0, 0, 0] [1, 0, 0, 1, 0, 0] [2, 2, 0, 1, 0, 0] [4, 2, 3, 4, 0, 0]
function result: 2
[2] {2: [0, 1, 2, 3, 4]} [1, 0, 0, 0, 0] [2, 2, 0, 0, 0]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
function result: 9
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8]} [1, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9]
function result: 10
[10] {10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0] [5, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0] [6, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0] [7, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0] [8, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0] [9, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0] [10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
function result: 4
[5, 4] {4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14], 5: [10]} [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [4, 2, 3, 4, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[2, 2, 2, 2, 2]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
print(squares)
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
height_locs = {}
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
squares = [0]*len(A)
biggestSquare = 0
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
heights = list(set(A))
heights.sort(reverse=True)
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
[2, 2, 2, 2, 2]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
[2, 2, 2, 2, 2]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
[2, 2, 2, 2, 2]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
[2, 2, 2, 2, 2]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4]
def solution(A):
biggestSquare = 0
height_locs = {}
squares = [0]*len(A)
heights = list(set(A))
heights.sort(reverse=True)
for i,a in enumerate(A):
if a in height_locs:
height_locs[a].append(i)
else:
height_locs[a] = [i]
for h in heights:
for idx in height_locs[h]:
if biggestSquare > h:
return biggestSquare
squares[idx] = 1
left = squares[idx-1] if idx > 0 else 0
if left > 0:
squares[idx - left] += 1
squares[idx] = squares[idx-left]
right = squares[idx+1] if idx < len(A)-1 else 0
if right > 0:
squares[idx + right] += squares[idx]
squares[idx - left] = squares[idx + right]
if squares[idx - left] >= biggestSquare:
biggestSquare = squares[idx-left]
if biggestSquare >= h:
return h
return max(squares)
The solution obtained perfect score.
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.