Surface Area of 3D Shapes - Problem
Surface Area of 3D Shapes

Imagine you're building with LEGO blocks! You have an n × n grid where each cell (i, j) contains a tower of grid[i][j] identical unit cubes stacked vertically. Each cube has dimensions 1 × 1 × 1.

When you place these cubes, adjacent cubes automatically stick together (like magnets!), forming irregular 3D shapes. Two cubes are considered adjacent if they share a face - this happens when cubes are next to each other horizontally, vertically, or stacked on top of each other.

Your Goal: Calculate the total surface area of all resulting 3D shapes.

Important: The bottom faces touching the ground also count toward the total surface area (imagine the ground is transparent and you can see underneath).

Example: If grid[0][0] = 2, you have 2 cubes stacked at position (0,0). The bottom cube contributes 5 faces to surface area (top face is hidden by the cube above), and the top cube contributes 5 faces (bottom face is hidden by the cube below).

Input & Output

example_1.py — Basic 2x2 Grid
$ Input: grid = [[2]]
Output: 10
💡 Note: Single tower with 2 cubes stacked. Each cube has 6 faces, total 12. The two cubes touch each other, hiding 2 faces (1 from each cube). Surface area = 12 - 2 = 10.
example_2.py — Adjacent Towers
$ Input: grid = [[1,2],[3,4]]
Output: 34
💡 Note: Four towers with heights 1,2,3,4. Each tower contributes faces minus the faces hidden by adjacent towers and internal stacking. Total calculation: (1×6-1-2) + (2×6-2-1-1) + (3×6-4-1-2) + (4×6-6-2-3) = 3+8+9+14 = 34.
example_3.py — Empty Grid
$ Input: grid = [[1,0],[0,2]]
Output: 16
💡 Note: Two towers at opposite corners. Tower at (0,0) has height 1: 6 faces total. Tower at (1,1) has height 2: 12-2=10 faces (2 internal faces hidden). No adjacent towers, so 6+10=16.

Constraints

  • 1 ≤ n ≤ 50 (grid is n × n)
  • 0 ≤ grid[i][j] ≤ 50
  • Each grid[i][j] represents the height of the tower at position (i,j)

Visualization

Tap to expand
3D Surface Area CalculationStep 1: Individual Cubes (6 faces each)66Step 2: Stack Cubes (lose internal faces)106-2 facesStep 3: Adjacent Connection (lose touching faces)Shared faces hiddenFormulaFor each tower at (i,j):1. Start: height × 6 faces2. Remove: (height-1) × 2 internal3. Remove: min(height, neighbor) × 1 for each of 4 neighborsTime: O(n²) | Space: O(1)
Understanding the Visualization
1
Place Individual Cubes
Each cube starts with 6 exposed faces
2
Stack Cubes Vertically
Touching faces become internal and don't count
3
Connect Adjacent Towers
Side faces of neighboring towers hide each other
4
Count Remaining Surface
Only exposed faces contribute to total surface area
Key Takeaway
🎯 Key Insight: Each cube contributes 6 faces initially, but we subtract exactly 2 faces for every adjacent relationship (vertical stacking or horizontal neighbors)
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 5
24.7K 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