-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path892.cpp
More file actions
26 lines (26 loc) · 851 Bytes
/
Copy path892.cpp
File metadata and controls
26 lines (26 loc) · 851 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
class Solution {
public:
int surfaceArea(vector<vector<int>> &grid) {
int x = grid.size(), y = x ? grid[0].size() : 0;
if (x == 0)
return 0;
int total = 0, area = 0;
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
if (grid[i][j] == 0)
continue;
area = 2 + 4 * grid[i][j];
if (i - 1 >= 0)
area -= min(grid[i - 1][j], grid[i][j]);
if (j - 1 >= 0)
area -= min(grid[i][j - 1], grid[i][j]);
if (i + 1 < x)
area -= min(grid[i + 1][j], grid[i][j]);
if (j + 1 < y)
area -= min(grid[i][j + 1], grid[i][j]);
total += area;
}
}
return total;
}
};