LeetCode: Maximal Rectangle

Maximum size square sub-matrix with all 1s

http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/

 

 1 public static int maximalRectangle(char[][] matrix) {
 2         int max = 0;
 3         int row = matrix.length; 
 4         int col = matrix[0].length;
 5         int[][] height = new int[row][col];
 6         for (int j = 0; j<col; j++) {
 7             height[0][j] = matrix[0][j]-'0';
 8         }
 9         for (int i=1; i<row; i++) {
10             for (int j=0; j<col; j++) {
11                 if (matrix[i][j] == '1') {
12                     height[i][j] = height[i-1][j]+1;
13                 }
14             }
15         }
16         
17         for (int i=0; i<row; i++) {
18             int area = largestRectangleArea(height[i]);
19             if (area > max) max = area;
20         }
21         
22         return max;
23     }
24     
25     public static int largestRectangleArea(int[] height) {
26         Stack<Integer> stack = new Stack<Integer>();
27         int i = 0;
28         int maxArea = 0;
29         int[] h = new int[height.length + 1];
30         h = Arrays.copyOf(height, height.length + 1);
31         while(i < h.length){
32             if(stack.isEmpty() || h[stack.peek()] <= h[i]){
33                 stack.push(i++);
34             }else {
35                 int t = stack.pop();
36                 maxArea = Math.max(maxArea, h[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
37             }
38         }
39         return maxArea;
40      }

http://www.cnblogs.com/lichen782/p/3196570.html

另外一种方法:

http://www.cnblogs.com/Rosanna/p/3527808.html

 

转载于:https://www.cnblogs.com/longhorn/p/3672854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值