-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path1277.cpp
77 lines (76 loc) · 2.21 KB
/
1277.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
__________________________________________________________________________________________________
sample 24 ms submission
static const auto ___ = [](){
std::cout.sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int n = matrix.size();
if(n==0) return 0;
int m = matrix[0].size();
int count = 0 ;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(matrix[i][j]>0 && i>0 && j>0)
matrix[i][j] = min(matrix[i-1][j],min(matrix[i][j-1],matrix[i-1][j-1]))+1;
count += matrix[i][j];
}
}
return count;
}
};
__________________________________________________________________________________________________
sample 28 ms submission
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
if(matrix.empty()){
return 0;
}
int r = matrix.size();
int c = matrix[0].size();
vector<vector<int>> dp(r+1, vector<int>(c+1));
int ret = 0;
for(int i=1; i<=r; i++){
for(int j=1; j<=c; j++){
if(matrix[i-1][j-1]){
dp[i][j] = 1 + min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]));
}
ret += dp[i][j];
}
}
return ret;
}
};
auto speedup=[](){
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return nullptr;
__________________________________________________________________________________________________
sample 32 ms submission
static const auto ___ = [](){
std::cout.sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
int countSquares(vector<vector<int>>& matrix) {
int n = matrix.size();
if(n==0) return 0;
int m = matrix[0].size();
int count = 0 ;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(matrix[i][j]>0 && i>0 && j>0)
matrix[i][j] = min(matrix[i-1][j],min(matrix[i][j-1],matrix[i-1][j-1]))+1;
count += matrix[i][j];
}
}
return count;
}
};