Leetcode 1277. 统计全为 1 的正方形子矩阵 动态规划

原题链接:Leetcode 1277. 统计全为 1 的正方形子矩阵

cpp 复制代码
class Solution {
public:
    int countSquares(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = 0;
                if (matrix[i][j] == 1) {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]),
                                       dp[i - 1][j - 1]) +
                                   1;
                    }
                }
                res += dp[i][j];
            }
        }
        return res;
    }
};
相关推荐
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll5 天前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐5 天前
leetcode-最小栈
java·算法·leetcode
zephyr055 天前
DP 从放弃到拿捏:一份持续更新的动态规划题解清单(一)
算法·动态规划
Frostnova丶5 天前
LeetCode 1356. 根据数字二进制下1的数目排序
数据结构·算法·leetcode
mjhcsp5 天前
C++轮廓线 DP:从原理到实战的深度解析
开发语言·c++·动态规划
.格子衫.5 天前
031动态规划之数位DP——算法备赛
算法·动态规划
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
样例过了就是过了5 天前
LeetCode热题100 环形链表 II
数据结构·算法·leetcode·链表