LeetCode: Maximal Rectangle 解题报告

Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

Show Tags
Have you met this question in a real interview? Yes  No
Discuss
SOLUTION 1:

 1 public class Solution {
 2     public int maximalRectangle(char[][] matrix) {
 3         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 4             return 0;
 5         }
 6         
 7         int rows = matrix.length;
 8         int cols = matrix[0].length;
 9         
10         int[][] h = new int[rows][cols];
11         
12         int max = 0;
13         
14         for (int i = 0; i < rows; i++) {
15             for (int j = 0; j < cols; j++) {
16                 
17                 h[i][j] = matrix[i][j] == '1' ? 1: 0;
18                 
19                 if (i != 0 && h[i][j] != 0) {
20                     h[i][j] = h[i - 1][j] + 1;
21                 }
22                 
23                 if (j == cols - 1) {
24                     max = Math.max(max, maxArea(h[i]));
25                 }
26             }
27         }
28         
29         return max;
30     }
31     
32     public int maxArea(int[] h) {
33         Stack<Integer> s = new Stack<Integer>();
34         
35         int max = 0;
36         int i = 0;
37         
38         // 注意,这里使用<= 因为当i到达最后,需要计算一次。
39         while (i <= h.length) {
40             // 
41             if (s.isEmpty() || i < h.length && h[i] >= h[s.peek()]) {
42                 s.push(i);
43                 i++;
44             } else {
45                 int height = h[s.pop()];
46                 int width = s.isEmpty() ? i: i - s.peek() - 1;
47                 max = Math.max(max, height * width);
48             }
49         }
50         
51         return max;
52     }
53 }

 2015.1.3 redo:

 1 public class Solution {
 2     public int maximalRectangle(char[][] matrix) {
 3         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 4             return 0;
 5         }
 6         
 7         int rows = matrix.length;
 8         int cols = matrix[0].length;
 9         int[][] h = new int[matrix.length][matrix[0].length];
10         
11         int maxArea = 0;
12         
13         for (int i = 0; i < rows; i++) {
14             for (int j = 0; j < cols; j++) {
15                 if (matrix[i][j] == '0') {
16                     h[i][j] = 0;
17                 } else {
18                     if (i == 0) {
19                         h[i][j] = 1;    
20                     } else {
21                         h[i][j] = 1 + h[i - 1][j];
22                     }
23                 }
24                 
25                 if (j == cols - 1) {
26                     // the last element of every row.
27                     int maxRec = maxAreaInRow(h[i]);
28                     maxArea = Math.max(maxArea, maxRec);
29                 }
30             }
31         }
32         
33         return maxArea;
34     }
35     
36     public int maxAreaInRow(int[] h) {
37         Stack<Integer> s = new Stack<Integer>();
38         
39         int i = 0;
40         int max = 0;
41         while (i <= h.length) {
42             // the current height is higher or equal than the one in the stack.
43             if (s.isEmpty() || i < h.length && h[i] >= h[s.peek()]) {
44                 s.push(i);
45                 i++;
46             } else {
47                 // get the area.
48                 int hight = h[s.pop()];
49                 int w = s.isEmpty() ? i: i - s.peek() - 1;
50                 max = Math.max(max, hight * w);
51             }
52         }
53         
54         return max;
55     }
56 }
View Code

 

我们可以一行一行扫描,记录下以每一个cell开始的最高的bar。

举例:

1001010

0101000

那么对第一行而言,cell的高度是1001010, 第二行是0102000

在每一行计算到尾部时,针对该行的计算Max Rectangle。方法是是与下题一致的。

https://oj.leetcode.com/submissions/detail/6375956/

附上写得不错的解释:

这个我不去debug下都特么不知道在干嘛。

那要不就debug下看看这段代码在做神马。例子就用题目中的[2,1,5,6,2,3]吧。

首先,如果栈是空的,那么索引i入栈。那么第一个i=0就进去吧。注意栈内保存的是索引,不是高度。然后i++。



然后继续,当i=1的时候,发现h[i]小于了栈内的元素,于是出栈。(由此可以想到,哦,看来stack里面只存放单调递增的索引)

这时候stack为空,所以面积的计算是h[t] * i.t是刚刚弹出的stack顶元素。也就是蓝色部分的面积。



继续。这时候stack为空了,继续入栈。注意到只要是连续递增的序列,我们都要keep pushing,直到我们遇到了i=4,h[i]=2小于了栈顶的元素。



这时候开始计算矩形面积。首先弹出栈顶元素,t=3。即下图绿色部分。



接下来注意到栈顶的(索引指向的)元素还是大于当前i指向的元素,于是出栈,并继续计算面积,桃红色部分。



最后,栈顶的(索引指向的)元素大于了当前i指向的元素,循环继续,入栈并推动i前进。直到我们再次遇到下降的元素,也就是我们最后人为添加的dummy元素0.



同理,我们计算栈内的面积。由于当前i是最小元素,所以所有的栈内元素都要被弹出并参与面积计算。



注意我们在计算面积的时候已经更新过了maxArea。

总结下,我们可以看到,stack中总是保持递增的元素的索引,然后当遇到较小的元素后,依次出栈并计算栈中bar能围成的面积,直到栈中元素小于当前元素。


 -------------------------------------------------更新----------------------------------------------------------------

可以这样理解这个算法,看下图。



例 如我们遇到最后遇到一个递减的bar(红色)。高度位于红线上方的(也就是算法中栈里面大于最右bar的)元素,他们是不可能和最右边的较小高度bar围 成一个比大于在弹栈过程中的矩形面积了(黄色面积),因为红色的bar对他们来说是一个短板,和红色bar能围成的最大面积也就是红色的高度乘以这些“上 流社会”所跨越的索引范围。但是“上流社会”的高度个个都比红色bar大,他们完全只计算彼此之间围成的面积就远远大于和红色bar围成的任意面积了。所 以红色bar是不可能参与“上流社会”的bar的围城的(好悲哀)。

但是屌丝也不用泄气哦。因为虽然长度不占优势,但是团结的力量是无穷的。它还可以参与“比较远的”比它还要屌丝的bar的围城。他们的面积是有可能超过上流社会的面积的,因为距离啊!所以弹栈到比红色bar小就停止了。

另外一个细节需要注意的是,弹栈过程中面积的计算。

h[t] * (stack.isEmpty() ? i : i - stack.peek() - 1)

h[t] 是刚刚弹出的栈顶端元素。此时的面积计算是h[t]和前面的“上流社会”能围成的最大面积。这时候要注意哦,栈内索引指向的元素都是比h[t]小的,如果 h[t]是目前最小的,那么栈内就是空哦。而在目前栈顶元素和h[t]之间(不包括h[t]和栈顶元素),都是大于他们两者的。如下图所示:



那h[t]无疑就是Stack.Peek和t之间那些上流社会的短板啦,而它们的跨越就是i - Stack.Peek - 1。

所以说,这个弹栈的过程也是维持程序不变量的方法啊:栈内元素一定是要比当前i指向的元素小的。

 

参考自: http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html

 

请至主页君的GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/MaximalRectangle.java

posted on 2014-11-19 08:29  Yu's Garden  阅读(1078)  评论(0编辑  收藏  举报

导航