# step 0: we use "find the max rectangle area with heights" algorithm, it also a hard problem 84. Largest Rectangle in Histogram # use the algorithm n times # step 1: keep the heights list, in crrent y, the highest rectangle height we can find. # step 2: every time finished the scan, we got a new height list. then we run "find the max rectangle area with heights" algorithm # step 3: keep result = max(result, current area) # step 4: return result
# time complexity: O(n * m) # space comlexity: (n) len(matrix[0])
y = m = len(matrix) x = n = len(matrix[0])
heights = [0] * x
result = 0
for cur_y inrange(y): for cur_x inrange(x): if matrix[cur_y][cur_x] == "1": heights[cur_x] += 1 else: heights[cur_x] = 0