您的位置:首页 > 其它

【LeetCode】Maximal Rectangle && Maximal Square

2015-06-05 17:56 519 查看
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]
;
        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]
;
        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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: