Surface Area of 3D Shapes - Problem
You are given an n x n grid where you have placed some 1 x 1 x 1 cubes. Each value v = grid[i][j] represents a tower of v cubes placed on top of cell (i, j).
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
Return the total surface area of the resulting shapes. Note: The bottom face of each shape counts toward its surface area.
Input & Output
Example 1 — Basic 2x2 Grid
$
Input:
grid = [[2]]
›
Output:
10
💡 Note:
Single tower with height 2 has 2×4=8 side faces + 1 top + 1 bottom = 10 total faces
Example 2 — Adjacent Towers
$
Input:
grid = [[1,2],[3,4]]
›
Output:
34
💡 Note:
Four towers with glued faces: Tower 1: 6 faces, Tower 2: 10 faces, Tower 3: 14 faces, Tower 4: 22 faces, minus hidden connections = 34
Example 3 — Empty and Full
$
Input:
grid = [[1,0],[0,2]]
›
Output:
16
💡 Note:
Two separate towers (1 and 2 height) with no connections: 6 + 10 = 16 faces
Constraints
- n == grid.length == grid[i].length
- 1 ≤ n ≤ 50
- 0 ≤ grid[i][j] ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
Each cell represents tower height
2
Build 3D Shape
Adjacent cubes glue together, hiding internal faces
3
Count Surface
Sum all exposed faces including bottom
Key Takeaway
🎯 Key Insight: Each cube has 6 faces, but adjacent cubes hide connecting faces - count only what's exposed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code