Skip to content

Commit 02e7084

Browse files
solves surface area of 3d shapes
1 parent 568fe5f commit 02e7084

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
| 883 | [Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes) | [![Java](assets/java.png)](src/ProjectionAreaOf3DShapes.java) |
241241
| 884 | [Uncommon Words from 2 Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences) | [![Java](assets/java.png)](src/UncommonWordsFromTwoSentences.java) |
242242
| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | [![Java](assets/java.png)](src/FairCandySwap.java) |
243-
| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes) | |
243+
| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes) | [![Java](assets/java.png)](src/SurfaceAreaOf3DShapes.java) |
244244
| 893 | [Groups of Special Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings) | |
245245
| 896 | [Monotonic Array](https://leetcode.com/problems/monotonic-array) | |
246246
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree) | |

Diff for: src/SurfaceAreaOf3DShapes.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class SurfaceAreaOf3DShapes {
2+
public int surfaceArea(int[][] grid) {
3+
final int rows = grid.length, columns = grid[0].length;
4+
int surfaceArea = 0;
5+
for (int row = 0 ; row < rows ; row++) {
6+
for (int column = 0 ; column < columns ; column++) {
7+
surfaceArea += (grid[row][column] > 0 ? 2 : 0)
8+
+ (column + 1 < columns ? Math.abs(grid[row][column] - grid[row][column + 1]) : grid[row][column])
9+
+ (row + 1 < rows ? Math.abs(grid[row][column] - grid[row + 1][column]) : grid[row][column])
10+
+ (row == 0 ? grid[row][column] : 0)
11+
+ (column == 0 ? grid[row][column] : 0);
12+
}
13+
}
14+
return surfaceArea;
15+
}
16+
}

0 commit comments

Comments
 (0)