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
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
ā Quadratic Growth
Space Complexity
O(n)
Arrays to store maximum values for each row and column
ā” Linearithmic Space
Constraints
-
n == grid.length == grid[i].length -
1 ⤠n ⤠50 -
0 ⤠grid[i][j] ⤠50
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code