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;
    }
};
相关推荐
Tisfy24 分钟前
LeetCode 0678.有效的括号字符串:O(n)+O(1)一次遍历
算法·leetcode·字符串·题解·贪心·括号匹配
hai31524754331 分钟前
文本矩阵化拆解与视频矩阵化构建
线性代数·矩阵·音视频
随意起个昵称11 小时前
【背包dp】输出方案路径
c++·动态规划
Navigator_Z13 小时前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
青山木16 小时前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
All for pursuit.19 小时前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode
随意起个昵称21 小时前
【分组背包】洛谷刷题合集
算法·动态规划
蝶依斓(湖南)全案整装1 天前
整装行业长尾需求内容矩阵搭建方法:从五个维度拆解用户需求
大数据·人工智能·矩阵
梦帮科技1 天前
推测解码(Speculative Decoding)与 Draft-Target 双核协同:接受率判据、树状验证与两倍吞吐无损加速
网络·人工智能·深度学习·神经网络·线性代数·矩阵·架构
All for pursuit.1 天前
【回溯-5】78.子集
数据结构·c++·算法·leetcode