|
| 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 | +} |
0 commit comments