leetcode85

 1 class Solution {
 2     public int maximalRectangle(char[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
 4         
 5         int[] height = new int[matrix[0].length];
 6         for(int i = 0; i < matrix[0].length; i ++){
 7             if(matrix[0][i] == '1') height[i] = 1;
 8         }
 9         int result = largestInLine(height);
10         for(int i = 1; i < matrix.length; i ++){
11             resetHeight(matrix, height, i);
12             result = Math.max(result, largestInLine(height));
13         }
14         
15         return result;
16     }
17 
18     private void resetHeight(char[][] matrix, int[] height, int idx){
19         for(int i = 0; i < matrix[0].length; i ++){
20             if(matrix[idx][i] == '1') height[i] += 1;
21             else height[i] = 0;
22         }
23     }    
24 
25     public int largestInLine(int[] height) {
26         if(height == null || height.length == 0) return 0;
27         int len = height.length;
28         Stack<Integer> s = new Stack<Integer>();
29         int maxArea = 0;
30         for(int i = 0; i <= len; i++){
31             int h = (i == len ? 0 : height[i]);
32             if(s.isEmpty() || h >= height[s.peek()]){
33                 s.push(i);
34             }else{
35                 int tp = s.pop();
36                 maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
37                 i--;
38             }
39         }
40         return maxArea;
41     }
42 }

参考:https://leetcode.com/problems/maximal-rectangle/discuss/29055/My-java-solution-based-on-Maximum-Rectangle-in-Histogram-with-explanation

转载于:https://www.cnblogs.com/asenyang/p/10488656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值