Ctrl K

Shadows of Block Towers

Easy
A square n × n pad holds towers of identical unit cubes: heights[i][j] is how many cubes are stacked in cell (i, j). Zero means the cell is empty. Shine a light on the structure from above, from the front (along the rows) and from the side (along the columns), and three shadows appear on three perpendicular walls. Each shadow is made of unit squares. Return the combined area of all three shadows.
Example 1
heights=[[3, 0], [1, 2]] → 13

From above, 3 cells hold cubes. From the front you see the row maxima: 3 + 2 = 5. From the side, the column maxima: 3 + 2 = 5. Total: 3 + 5 + 5 = 13.

Example 2
heights=[[4]] → 9

A single tower of height 4: 1 from above, 4 from the front, 4 from the side.

Example 3
heights=[[0, 0], [0, 0]] → 0

No cubes, no shadows.

Constraints
  • n == len(heights) == len(heights[i])
  • 1 ≤ n ≤ 50
  • 0 ≤ heights[i][j] ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSurface of Cube Towers EasyHow a Rumor Spreads EasyThe Biggest Triangle on the Board EasyThree Stakes Off the Line EasyPosts in One Line Easy
loading Python…
Input
heights=[[3, 0], [1, 2]]
Expected
13

Run checks the first 3 tests in your browser. Submit checks all 75 tests on the server, hidden ones included.