[leetcode] 221. 最大正方形

221. 最大正方形

其实这题是85. 最大矩形的特殊情况,我们将85题代码稍微改一下,然后直接套用即可。

此题要求是正方形,那么我们在计算长与宽时,取短的那条然后平方即可。

class Solution {
    public int maximalSquare(char[][] matrix) {
        return maximalRectangle(matrix);
    }

    public int maximalRectangle(char[][] matrix) {
        int m = matrix.length;
        if (m == 0) return 0;
        int n = matrix[0].length;
        if (n == 0) return 0;

        int[] height = new int[n];
        int ans = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == ‘0‘) height[j] = 0;
                else if (matrix[i][j] == ‘1‘) height[j] += 1;
            }
            ans = Math.max(largestRectangleArea(height), ans);
        }
        return ans;
    }

    public int largestRectangleArea(int[] heights) {
        if (heights.length == 0) return 0;
        Stack<Integer> stack = new Stack<>();

        int max = 0;
        for (int i = 0; i < heights.length; i++) {
            while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
                int tmp = stack.pop();
                // 把当前的tmp木板作为最短木板,看能组成的最大面积是多少
                int bian = Math.min(heights[tmp], (stack.empty() ? i : i - stack.peek() - 1));
//                max = Math.max(max, heights[tmp] * (stack.empty() ? i : i - stack.peek() - 1));
                max = Math.max(max, bian * bian);
            }
            stack.push(i);
        }

        int tmp = 0;
        int len = heights.length;
        while (!stack.isEmpty()) {
            tmp = stack.pop();
            int bian = Math.min(heights[tmp], (stack.empty() ? len : len - stack.peek() - 1));
//            max = Math.max(max, heights[tmp] * (stack.empty() ? len : len - stack.peek() - 1));
            max = Math.max(max, bian * bian);
        }

        return max;
    }
}

原文地址:https://www.cnblogs.com/acbingo/p/9404167.html

时间: 2024-07-31 14:58:33

[leetcode] 221. 最大正方形的相关文章

[LeetCode] 221. 最大正方形(DP)

题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximal-square 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 题解 dp[i][j] 表示以matrix[i-1][j-1]为右下角的最大正方形的边长 转移方

[LeetCode] 221. Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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 addin

Leetcode 221.最大的正方形

最大的正方形 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了.这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情

(DP 线性DP) leetcode 221. Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 =============================================================== 这是一个D

leetcode 火柴拼正方形 深搜

还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到. 输入为小女孩拥有火柴的数目,每根火柴用其长度表示.输出即为是否能用所有的火柴拼成正方形. 示例?1: 输入: [1,1,2,2,2] 输出: true 解释: 能拼成一个边长为2的正方形,每边两根火柴. 示例?2: 输入: [3,3,3,3,4] 输出: false 解释: 不能用所有火柴拼成一个正方形. 来源:力扣(LeetC

(medium)LeetCode 221.Maximal Square

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

Leetcode 593.有效正方形

有效正方形 给定二维空间中四点的坐标,返回四点是否可以构造一个正方形. 一个点的坐标(x,y)由一个有两个整数的整数数组表示. 示例: 输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] 输出: True 注意: 所有输入整数都在 [-10000,10000] 范围内. 一个有效的正方形有四个等长的正长和四个等角(90度角). 输入点没有顺序. 1 public class Solution { 2 public double dist(int[

leetcode 221

题意:给定一个0,1矩阵,找到最大的包含1的正方形,并返回它的面积. 思路:动态规划 初始化:二维数组:dp[i][j] 表示 到达(i, j )位置所能组成的最大正方形的边长. 1)边界条件:i表示行数,j表示列数. i == 0 || j == 0 2)状态转移方程:matrix[i][j] == 1时,dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j] ) ) +1 class Solution { public: int max

221. 最大正方形

题目: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 输出: 4 思路: 动态规划,时间复杂度为O(n^2)dp[i][j]表示以点(i, j)为右下角的正方形的最大边长:状态转移方程:dp[i][j] = min(min(dp[i - 1][j - 1], dp[i][j-1]),min(dp[i-1][j - 1], dp[i - 1][j])) + 1; 当我们