Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.
Example 1:
Input: mat = [[1,0,1],
[1,1,0],
[1,1,0]]
Output: 13
Explanation:
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
Example 2:
Input: mat = [[0,1,1,0],
[0,1,1,1],
[1,1,1,0]]
Output: 24
Explanation:
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3.
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
classSolution:deflargestRectangleArea(self,heights: List[int]) ->int: s, res = [],0 heights.append(0)for i, h inenumerate(heights):whilelen(s)>0and h < heights[s[-1]]: hei = heights[s[-1]] s.pop() wid = i - s[-1]-1iflen(s)>0else i res =max(res, hei * wid) s.append(i)return res
和上个解法同理,从右往左遍历k in [j, 0],mi为当前遍历的最短高度,res += mi(k) for all k。此时如果能统计出以heights[j]向左最远能到的位置如peek,再加上peek前的结果也就是对j列所求。比如图中最后一列(2+2+2 + 3)前面三个2为之前第3列所求,最后的3为当前列的高度*它对应的宽度1。让dp[k]记录第k列所增加的新长方形数目。求最远能扩所少想起Largest Rectangle in Histogram,类似的按高度递增的stack内记录index,不同在于这次宽度不是对每个弹出的元素做计算,而是所有弹出后计算i和peek的diff:
classSolution:defnumSubmat(self,mat: List[List[int]]) ->int: M, N, res =len(mat),len(mat[0]),0 height = [0] * Ndeflevel(height: []): stack, res, dp = [],0, [0] * Nfor i, h inenumerate(height):whilelen(stack)>0and h < height[stack[-1]]: stack.pop()iflen(stack)>0: dp[i]= dp[stack[-1]] dp[i]+= h * (i - stack[-1]) else: dp[i]= h * (i +1) stack.append(i) res =sum(dp)return resfor i inrange(M):for j inrange(N): height[j]= height[j]+1if mat[i][j] ==1else0 res +=level(height)return res