forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
22 lines (22 loc) · 671 Bytes
/
s2.cpp
File metadata and controls
22 lines (22 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// OJ: https://leetcode.com/problems/count-square-submatrices-with-all-ones/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(N)
class Solution {
public:
int countSquares(vector<vector<int>>& A) {
int M = A.size(), N = A[0].size(), ans = 0;
vector<int> dp(N + 1);
for (int i = 0; i < M; ++i) {
int prev = 0;
for (int j = 0; j < N; ++j) {
int cur = dp[j + 1];
if (A[i][j] == 0) dp[j + 1] = 0;
else dp[j + 1] = min({ prev, dp[j], dp[j + 1] }) + 1;
ans += dp[j + 1];
prev = cur;
}
}
return ans;
}
};