【LeetCode】Maximal Rectangle && Maximal Square

133 篇文章 0 订阅
121 篇文章 2 订阅
1、 Maximal Rectangle 
Total Accepted: 24875 Total Submissions: 113379 My Submissions Question Solution 
Given a 2D binary matrix filled with 0's and 1's, 
find the largest rectangle containing all ones and return its area.
2、Maximal Square 
Total Accepted: 1488 Total Submissions: 7283 My Submissions Question Solution 
Given a 2D binary matrix filled with 0's and 1's, 
find the largest square containing all 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

【解题思路】
参考 Largest Rectangle in Histogram,解题思路参考 【LeetCode】Largest Rectangle in Histogram
针对二维矩阵的每行,其实就是一个单独的Largest Rectangle。
针对长方形,思路和Largest Rectangle一致。
针对正方形,每次计算square的时候,取长和宽最小值的平方即为面积。

1 Java AC

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if(matrix == null || matrix.length == 0){
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int height[][] = new int[m][n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == '0'){
                    height[i][j] = 0;
                }else{
                    height[i][j] = i == 0 ? 1 : height[i-1][j] + 1;
                }
            }
        }
        int maxArea = 0;
        for(int i = 0; i < m; i++){
            int area = largestRectangleArea(height[i]);
            maxArea = area > maxArea ? area : maxArea;
        }
        return maxArea;
    }
    
    public int largestRectangleArea(int[] height) {
        if(height == null || height.length == 0){
            return 0;
        }
        int len = height.length;
        Stack<Integer> stack = new Stack<Integer>();
        int area = 0;
        for(int i = 0; i <= len; i++){
            int h = i == len ? 0 : height[i];
            if(stack.isEmpty() || height[stack.peek()] <= h){
                stack.push(i);
                continue;
            }
            int start = stack.pop();
            int width = stack.empty() ? i : i - stack.peek() - 1;  
            area = Math.max(area, height[start] * width);  
            i--;  
        }
        return area;
    }
}
2 Java AC

public class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix == null || matrix.length == 0){
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int height[][] = new int[m][n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == '0'){
                    height[i][j] = 0;
                }else{
                    height[i][j] = i == 0 ? 1 : height[i-1][j] + 1;
                }
            }
        }
        int maxArea = 0;
        for(int i = 0; i < m; i++){
            int area = largestRectangleArea(height[i]);
            maxArea = area > maxArea ? area : maxArea;
        }
        return maxArea;
    }
    
    public int largestRectangleArea(int[] height) {
        if(height == null || height.length == 0){
            return 0;
        }
        int len = height.length;
        Stack<Integer> stack = new Stack<Integer>();
        int area = 0;
        for(int i = 0; i <= len; i++){
            int h = i == len ? 0 : height[i];
            if(stack.isEmpty() || height[stack.peek()] <= h){
                stack.push(i);
                continue;
            }
            int start = stack.pop();
            int width = stack.empty() ? i : i - stack.peek() - 1;
            int min = Math.min(height[start], width);
            area = Math.max(area, min*min);  
            i--;  
        }
        return area;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值