Skip to content

Commit

Permalink
Create surface-area-of-3d-shapes.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Aug 26, 2018
1 parent c1d22c9 commit c4a77bf
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions C++/surface-area-of-3d-shapes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time: O(n^2)
// Space: O(1)

class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
int result = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid.size(); ++j) {
if (grid[i][j]) {
result += grid[i][j] * 4 + 2;
}
if (i) {
result -= min(grid[i][j], grid[i - 1][j]) * 2;
}
if (j) {
result -= min(grid[i][j], grid[i][j - 1]) * 2;
}
}
}
return result;
}
};

0 comments on commit c4a77bf

Please sign in to comment.