Projection Area of 3D Shapes - Problem

You are given an n x n grid where we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z axes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of the cell (i, j).

We view the projection of these cubes onto the xy, yz, and zx planes. A projection is like a shadow, that maps our 3-dimensional figure to a 2-dimensional plane. We are viewing the "shadow" when looking at the cubes from the top, the front, and the side.

Return the total area of all three projections.

Input & Output

Example 1 — Basic 2x2 Grid
$ Input: grid = [[1,2],[3,1]]
Output: 14
💡 Note: XY projection (top view): 4 non-zero cells. YZ projection (front view): max(1,2) + max(3,1) = 2 + 3 = 5. ZX projection (side view): max(1,3) + max(2,1) = 3 + 2 = 5. Total = 4 + 5 + 5 = 14.
Example 2 — Single Row
$ Input: grid = [[2]]
Output: 5
💡 Note: XY projection: 1 non-zero cell. YZ projection: max height in row = 2. ZX projection: max height in column = 2. Total = 1 + 2 + 2 = 5.
Example 3 — With Zero Heights
$ Input: grid = [[1,0],[0,2]]
Output: 8
💡 Note: XY projection: 2 non-zero cells. YZ projection: max(1,0) + max(0,2) = 1 + 2 = 3. ZX projection: max(1,0) + max(0,2) = 1 + 2 = 3. Total = 2 + 3 + 3 = 8.

Constraints

  • n == grid.length == grid[i].length
  • 1 ≤ n ≤ 50
  • 0 ≤ grid[i][j] ≤ 50

Visualization

Tap to expand
3D Projection Problem: grid = [[1,2],[3,1]]3D Cube Towers1231XY (Top View)Area = 4YZ (Front View)max=2max=3Area = 5ZX (Side View)max=3max=2Area = 5Total Projection Area = 4 + 5 + 5 = 14Each projection represents the shadow cast from that viewing angle
Understanding the Visualization
1
Input Grid
Each cell represents a tower of cubes with given height
2
Three Projections
Calculate shadow areas from top, front, and side views
3
Sum Areas
Total area of all three projections
Key Takeaway
🎯 Key Insight: Think of projections as shadows - top view counts occupied spaces, front/side views show maximum heights
Asked in
Amazon 15 Google 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen