|
| 1 | +/** |
| 2 | + * On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. |
| 3 | + * |
| 4 | + * Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j). |
| 5 | + * |
| 6 | + * Now we view the projection of these cubes onto the xy, yz, and zx planes. |
| 7 | + * |
| 8 | + * A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane. |
| 9 | + * |
| 10 | + * Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side. |
| 11 | + * |
| 12 | + * Return the total area of all three projections. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * Input: [[2]] |
| 16 | + * Output: 5 |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * Input: [[1,2],[3,4]] |
| 20 | + * Output: 17 |
| 21 | + * Explanation: |
| 22 | + * Here are the three projections ("shadows") of the shape made with each axis-aligned plane. |
| 23 | + * https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png |
| 24 | + * |
| 25 | + * Example 3: |
| 26 | + * Input: [[1,0],[0,2]] |
| 27 | + * Output: 8 |
| 28 | + * |
| 29 | + * Example 4: |
| 30 | + * Input: [[1,1,1],[1,0,1],[1,1,1]] |
| 31 | + * Output: 14 |
| 32 | + * |
| 33 | + * Example 5: |
| 34 | + * Input: [[2,2,2],[2,1,2],[2,2,2]] |
| 35 | + * Output: 21 |
| 36 | + * |
| 37 | + * Note: |
| 38 | + * 1 <= grid.length = grid[0].length <= 50 |
| 39 | + * 0 <= grid[i][j] <= 50 |
| 40 | + */ |
| 41 | + |
| 42 | + |
| 43 | +publicclassProjectionAreaOf3DShapes887 { |
| 44 | +publicintprojectionArea(int[][]grid) { |
| 45 | +intM =grid.length; |
| 46 | +intN =grid[0].length; |
| 47 | +inttop =0; |
| 48 | +intside =0; |
| 49 | +int[]maxFront =newint[N]; |
| 50 | + |
| 51 | +for (inti=0;i<M;i++) { |
| 52 | +introwMax =0; |
| 53 | +for (intj=0;j<N;j++) { |
| 54 | +intnow =grid[i][j]; |
| 55 | +if (now >0)top++; |
| 56 | +if (now >rowMax)rowMax =now; |
| 57 | +if (now >maxFront[j])maxFront[j] =now; |
| 58 | + } |
| 59 | +side +=rowMax; |
| 60 | + } |
| 61 | + |
| 62 | +intfront =0; |
| 63 | +for (intcol:maxFront) { |
| 64 | +front +=col; |
| 65 | + } |
| 66 | +returntop +front +side; |
| 67 | + } |
| 68 | + |
| 69 | +} |