leetcode-221:最大正方形

简介: leetcode-221:最大正方形

题目

题目连接

在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。

示例 1:

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4

示例 2:

输入:matrix = [["0","1"],["1","0"]]
输出:1

示例 3:

输入:matrix = [["0"]]
输出:0

解题

方法一:动态规划

参考链接

就像 木桶的短板理论 那样——附近的最小边长,才与 ? 的最长边长有关。

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int m=matrix.size(),n=matrix[0].size();
        vector<vector<int>> dp(m+1,vector<int>(n+1));
        int maxLen=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(matrix[i][j]=='1'){
                    dp[i+1][j+1]=min({dp[i][j],dp[i+1][j],dp[i][j+1]})+1;
                    maxLen=max(maxLen,dp[i+1][j+1]);
                }
            }
        }
        return maxLen*maxLen;
    }
};
相关文章
|
2月前
|
存储
leetcode2975. 移除栅栏得到的正方形田地的最大面积
leetcode2975. 移除栅栏得到的正方形田地的最大面积
20 1
|
5月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 221. 最大正方形 算法解析
☆打卡算法☆LeetCode 221. 最大正方形 算法解析
|
4月前
leetcode-593:有效的正方形
leetcode-593:有效的正方形
18 0
|
4月前
|
Go
golang力扣leetcode 221.最大正方形
golang力扣leetcode 221.最大正方形
20 0
|
4月前
leetcode-1725:可以形成最大正方形的矩形数目
leetcode-1725:可以形成最大正方形的矩形数目
17 0
|
4月前
|
算法
leetcode-2013:检测正方形
leetcode-2013:检测正方形
26 0
|
4月前
|
Go
golang力扣leetcode 2013.检测正方形
golang力扣leetcode 2013.检测正方形
20 0
LeetCode-593 有效的正方形
LeetCode-593 有效的正方形
|
9月前
|
算法
LeetCode-2013 检测正方形
LeetCode-2013 检测正方形
LeetCode 221. 最大正方形
LeetCode 221. 最大正方形
57 0
LeetCode 221. 最大正方形

热门文章

最新文章