085 Maximal Rectangle
这道题主要是使用084的方法, 唯一要做的是处理一下输入 ,具体代码如下 复杂度O(n*n)
1 class Solution: 2 # @param {character[][]} matrix 3 # @return {integer} 4 def maximalRectangle(self, matrix): 5 ans = 0 6 for i in range(0,len(matrix)): 7 for j in range(0, len(matrix[0])): 8 if i == 0: 9 matrix[i][j] = int(matrix[i][j]) 10 else: 11 if matrix[i][j] == "0": 12 matrix[i][j] = 0 13 else: 14 matrix[i][j] = matrix[i-1][j] + 1 15 for m in matrix: 16 ans = max(ans, self.largestRectangleArea(m)) 17 return ans 18 19 def largestRectangleArea(self, height): 20 ans = 0 21 s = [] 22 for i in range(0, len(height)): 23 left = i 24 while (s != []) and (s[-1][0] > height[i]): 25 left = s[-1][1] 26 ans = max(ans, (i - left) * s[-1][0]) 27 s.pop() 28 s.append([height[i], left]) 29 right = len(height) 30 while s != []: 31 left = s[-1][1] 32 ans = max(ans, (right - left) * s[-1][0]) 33 s.pop() 34 return ans