-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path892.cpp
More file actions
29 lines (27 loc) · 771 Bytes
/
892.cpp
File metadata and controls
29 lines (27 loc) · 771 Bytes
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
class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
int n = grid.size();
int res = 0;
for (int i = 0; i < n; ++i) {
res += grid[i][0];
res += grid[i][n - 1];
for (int j = 1; j < n; ++j) {
res += abs(grid[i][j] - grid[i][j - 1]);
}
}
for (int j = 0; j < n; ++j) {
res += grid[0][j];
res += grid[n - 1][j];
for (int i = 1; i < n; ++i) {
res += abs(grid[i][j] - grid[i - 1][j]);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j]) res += 2;
}
}
return res;
}
};