-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurfaceAreaof3DShapes.cpp
More file actions
28 lines (27 loc) · 910 Bytes
/
surfaceAreaof3DShapes.cpp
File metadata and controls
28 lines (27 loc) · 910 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
// Source: https://leetcode.com/problems/surface-area-of-3d-shapes/
// Author: Miao Zhang
// Date: 2021-03-22
class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
vector<vector<int>> dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int res = 0;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid.size(); j++) {
if (grid[i][j]) {
res += 2;
for (auto& d: dirs){
int x = i + d[0];
int y = j + d[1];
int val = 0;
if (x >= 0 && x < grid.size() && y >= 0 && y < grid.size()) {
val = grid[x][y];
}
res += max(grid[i][j] - val, 0);
}
}
}
}
return res;
}
};