Projection Area of 3D Shapes - Problem

Imagine you have a grid where you're stacking cubes to build 3D towers! šŸ—ļø

Given an n Ɨ n grid, each cell grid[i][j] contains a value representing the height of a tower of unit cubes placed at position (i, j). Think of it like a 3D city skyline made of blocks!

Now, imagine shining light from three different directions to create shadows (projections) on three planes:

  • XY-plane (top view): Looking down from above
  • YZ-plane (side view): Looking from the right side
  • ZX-plane (front view): Looking from the front

Your task is to calculate the total area of all three shadow projections combined.

Key insight: A projection shows the "footprint" or "silhouette" of the 3D structure when viewed from that direction.

Input & Output

example_1.py — Basic 2x2 Grid
$ Input: grid = [[1,2],[3,4]]
› Output: 17
šŸ’” Note: XY projection: 4 cells (all non-zero), YZ projection: max(1,2) + max(3,4) = 2+4 = 6, ZX projection: max(1,3) + max(2,4) = 3+4 = 7. Total: 4+6+7 = 17
example_2.py — Grid with Zeros
$ Input: grid = [[2,0],[0,1]]
› Output: 8
šŸ’” Note: XY projection: 2 cells (only positions with towers), YZ projection: max(2,0) + max(0,1) = 2+1 = 3, ZX projection: max(2,0) + max(0,1) = 2+1 = 3. Total: 2+3+3 = 8
example_3.py — Single Cell
$ Input: grid = [[1]]
› Output: 3
šŸ’” Note: XY projection: 1 cell, YZ projection: max(1) = 1, ZX projection: max(1) = 1. Total: 1+1+1 = 3

Visualization

Tap to expand
XY: Area = 4YZ: Area = 7ZX: Area = 6Total Projection Area = 4 + 7 + 6 = 17
Understanding the Visualization
1
Build 3D Structure
Visualize towers of cubes based on grid values
2
XY Projection (Top View)
Shadow from above shows occupied footprint
3
YZ Projection (Side View)
Shadow from side shows row silhouettes
4
ZX Projection (Front View)
Shadow from front shows column silhouettes
Key Takeaway
šŸŽÆ Key Insight: Each projection captures a different dimension of the 3D structure - footprint, side silhouette, and front silhouette. Calculate all three in one efficient pass!

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

Single pass through the nƗn grid

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Arrays to store maximum values for each row and column

n
2n
⚔ Linearithmic Space

Constraints

  • n == grid.length == grid[i].length
  • 1 ≤ n ≤ 50
  • 0 ≤ grid[i][j] ≤ 50
Asked in
Google 15 Amazon 8 Microsoft 5 Apple 3
25.6K Views
Low Frequency
~12 min Avg. Time
890 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