Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit abc295e

Browse files
committedNov 28, 2021
solves projection area of 3d shapes
1 parent 9d50c37 commit abc295e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
| 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees) | [![Java](assets/java.png)](src/LeafSimilarTrees.java) |
238238
| 874 | [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation) | |
239239
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [![Java](assets/java.png)](src/MiddleOfTheLinkedList.java) |
240-
| 883 | [Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes) | |
240+
| 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) | |
242242
| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | |
243243
| 892 | [Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes) | |

‎src/ProjectionAreaOf3DShapes.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
3+
public class ProjectionAreaOf3DShapes {
4+
public int projectionArea(int[][] grid) {
5+
int area = 0;
6+
final int n = grid.length;
7+
8+
// z axis
9+
for (int row = 0 ; row < n ; row++) {
10+
for(int column = 0 ; column < n ; column++) {
11+
area += grid[row][column] > 0 ? 1 : 0;
12+
}
13+
}
14+
15+
// x axis
16+
for (int[] row : grid) {
17+
area += Arrays.stream(row).max().getAsInt();
18+
}
19+
20+
// y axis
21+
for (int column = 0 ; column < n ; column++) {
22+
int columnMax = 0;
23+
for (int row = 0 ; row < n ; row++) columnMax = Math.max(columnMax, grid[row][column]);
24+
area += columnMax;
25+
}
26+
27+
return area;
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.