Skip to content

Commit 8d43b4b

Browse files
add 883
1 parent da2e739 commit 8d43b4b

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 883. Projection Area of 3D Shapes
5+
*
6+
* On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
7+
* Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
8+
* Now we view the projection of these cubes onto the xy, yz, and zx planes.
9+
* A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
10+
* Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
11+
* Return the total area of all three projections.
12+
*
13+
* Example 1:
14+
* Input: [[2]]
15+
* Output: 5
16+
*
17+
* Example 2:
18+
* Input: [[1,2],[3,4]]
19+
* Output: 17
20+
* Explanation:
21+
* Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
22+
*
23+
* Example 3:
24+
* Input: [[1,0],[0,2]]
25+
* Output: 8
26+
*
27+
* Example 4:
28+
* Input: [[1,1,1],[1,0,1],[1,1,1]]
29+
* Output: 14
30+
*
31+
* Example 5:
32+
* Input: [[2,2,2],[2,1,2],[2,2,2]]
33+
* Output: 21
34+
*
35+
* Note:
36+
* 1 <= grid.length = grid[0].length <= 50
37+
* 0 <= grid[i][j] <= 50
38+
* */
39+
public class _883 {
40+
public static class Solution1 {
41+
/**credit: https://leetcode.com/problems/projection-area-of-3d-shapes/discuss/156726/C%2B%2BJavaPython-Straight-Forward-One-Pass*/
42+
public int projectionArea(int[][] grid) {
43+
int n = grid.length;
44+
int area = 0;
45+
for (int i = 0; i < n; i++) {
46+
int x = 0;
47+
int y = 0;
48+
for (int j = 0; j < n; j++) {
49+
x = Math.max(x, grid[i][j]);
50+
y = Math.max(x, grid[j][i]);
51+
if (grid[i][j] > 0) {
52+
area++;
53+
}
54+
}
55+
area += x + y;
56+
}
57+
return area;
58+
}
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._883;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _883Test {
10+
private static _883.Solution1 solution1;
11+
private static int[][] grid;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _883.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
grid = new int[][]{
21+
{2}
22+
};
23+
assertEquals(5, solution1.projectionArea(grid));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
grid = new int[][]{
29+
{1, 2},
30+
{3, 4},
31+
};
32+
assertEquals(17, solution1.projectionArea(grid));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)