Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Surface Area of 3D Shapes

XPChallenge Points: 10
levelLevel: Easy

You are given an n x n matrix where each entry represents a vertical stack of 1x1x1 cubes.
For a cell (i, j), the value grid[i][j] indicates how many cubes are piled on that position.

Cubes that are directly touching each other (in the same stack or adjacent horizontally) are considered glued, forming complex 3D shapes.

Your task is to compute the total exposed surface area of all these glued shapes combined.
Each cube face that is not in contact with another cube contributes to the surface area.
Do not forget that the bottom faces count as well.

Example 1:

Input: grid = [[2,1],[0,3]]

Output: 30

Explanation:

Considering all exposed faces, the overall visible surface adds up to 30.

Example 2:

Input: grid = [[1,0,2],[3,1,0],[2,2,1]]

Output: 44

Explanation:

Multiple towers of varying heights produce a total exposed surface of 44.

Example 3:

Input: grid = [[0,0],[0,5]]

Output: 22

Explanation:

Only one tall column contributes to the entire surface area.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result