← Home

883. Projection Area of 3D Shapes

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
883

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 883. Projection Area of 3D Shapes, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are projectionArea.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
03
04Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
05
06Now we view the projection of these cubes onto the xy, yz, and zx planes.
07
08A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane. 
09
10Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
11
12Return the total area of all three projections.
13
14 
15
16Example 1:
17
18Input: [[2]]
19Output: 5
20Example 2:
21
22Input: [[1,2],[3,4]]
23Output: 17
24Explanation: 
25Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
26
27Example 3:
28
29Input: [[1,0],[0,2]]
30Output: 8
31Example 4:
32
33Input: [[1,1,1],[1,0,1],[1,1,1]]
34Output: 14
35Example 5:
36
37Input: [[2,2,2],[2,1,2],[2,2,2]]
38Output: 21
39 
40
41Note:
42
431 <= grid.length = grid[0].length <= 50
440 <= grid[i][j] <= 50
45**/
46
47//Your runtime beats 99.47 % of cpp submissions.
48class Solution {
49public:
50    int projectionArea(vector<vector<int>>& grid) {
51        int xy = 0;
52        for(int i = 0; i < grid.size(); i++){
53            for(int j = 0; j < grid[0].size(); j++){
54                if(grid[i][j]>0) xy++;
55            }
56        }
57        int xz = 0;
58        for(int i = 0; i < grid.size(); i++){
59            xz+= *max_element(grid[i].begin(), grid[i].end());
60        }
61        int yz = 0;
62        for(int j = 0; j < grid[0].size(); j++){
63            int col_max = 0;
64            for(int i = 0; i < grid.size(); i++){
65                col_max = max(col_max, grid[i][j]);
66            }
67            yz+=col_max;
68        }
69        // cout << xy << " " << xz << " " << yz << endl;
70        return xy+xz+yz;
71    }
72};
73
74/**
75Approach 1: Mathematical
76Intuition and Algorithm
77
78From the top, the shadow made by the shape will be 1 square for each non-zero value.
79
80From the side, the shadow made by the shape will be the largest value for each row in the grid.
81
82From the front, the shadow made by the shape will be the largest value for each column in the grid.
83
84Example
85
86With the example [[1,2],[3,4]]:
87
88The shadow from the top will be 4, since there are four non-zero values in the grid;
89
90The shadow from the side will be 2 + 4, since the maximum value of the first row is 2, and the maximum value of the second row is 4;
91
92The shadow from the front will be 3 + 4, since the maximum value of the first column is 3, and the maximum value of the second column is 4.
93**/
94
95//Your runtime beats 99.47 % of cpp submissions.
96class Solution {
97public:
98    int projectionArea(vector<vector<int>>& grid) {
99        int ans = 0;
100        int N = grid.size();
101        for(int i = 0; i < N; i++){
102            int rowBest = 0;
103            int colBest = 0;
104            for(int j = 0; j < N; j++){
105                if(grid[i][j]>0) ans++;
106                rowBest = max(rowBest, grid[i][j]);
107                //since grid is a square, so we can do this
108                colBest = max(colBest, grid[j][i]);
109            }
110            ans+=rowBest+colBest;
111        }
112        return ans;
113    }
114};
115
116/**
117Complexity Analysis
118
119Time Complexity: O(N^2), where N is the length of grid.
120
121Space Complexity: O(1). 
122**/

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.