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.
Example 1:
Input: grid = [[1,2],[3,4]] Output: 34
Example 2:
Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 32
Example 3:
Input: grid = [[2,2,2],[2,1,2],[2,2,2]] Output: 46
Constraints:
n == grid.length == grid[i].length1 <= n <= 500 <= grid[i][j] <= 50Problem Overview: You are given an m x n grid where grid[i][j] represents the number of unit cubes stacked at that position. The task is to compute the total surface area of the resulting 3D structure. Adjacent cubes share faces, so internal faces must not be counted.
The grid acts like a height map. Each stack forms a vertical column of cubes. A single cube contributes 6 faces, but whenever two cubes touch, the touching faces disappear from the external surface. The goal is to count only the faces visible from the outside.
Approach 1: Direct Calculation of Surface Area (O(m*n) time, O(1) space)
Iterate through every cell of the grid and treat each stack independently. A stack with height h contributes 4*h + 2 surface area if it stands alone: four vertical sides plus top and bottom. The key adjustment comes from neighboring stacks. If the current stack touches another stack horizontally, the touching faces are hidden. Subtract 2 * min(currentHeight, neighborHeight) for each adjacent neighbor (typically checking top and left neighbors avoids double counting). This method works well because it directly models how shared faces reduce the exposed surface. The algorithm scans the grid once, performing constant-time arithmetic per cell.
This approach relies on simple iteration over a matrix while applying arithmetic rules derived from geometry. It avoids building any additional structures and keeps the logic compact.
Approach 2: Iterative Side-by-Side Calculation (O(m*n) time, O(1) space)
Another way to think about the problem is to count visible faces from each direction instead of starting with a full formula. Iterate through the grid and add the top and bottom surfaces whenever grid[i][j] > 0. Then compute the exposed sides by comparing the current stack with its neighbors. For example, the visible surface on the left side equals max(0, grid[i][j] - grid[i][j-1]). Repeat this for right, front, and back directions. Each comparison measures how much of the current stack extends beyond its neighbor.
This perspective mirrors how you would visually inspect the shape layer by layer. It’s still a simple scan over a array-based grid, but instead of subtracting overlaps, it adds only the exposed differences between adjacent stacks.
Recommended for interviews: The direct calculation approach is usually what interviewers expect. It demonstrates that you recognize shared faces between neighboring stacks and can translate the geometric observation into code. The side-by-side comparison method is equally efficient but emphasizes reasoning about exposed edges. Showing either approach with O(m*n) time and O(1) space signals solid understanding of grid traversal and surface counting.
This approach involves calculating the total surface area by starting from the maximum theoretical surface area (each cube contributing six sides) and subtracting the overlapping sides between adjacent cubes.
This C solution iterates through each cell in the grid. For each cube, it accounts for all six sides and subtracts the shared face between adjacent cubes. The use of fmin ensures we only subtract surface area up to the height of the shorter stack.
Time Complexity: O(n^2), where n is the dimension of the grid.
Space Complexity: O(1), as no additional space proportional to the input size is used.
This approach focuses on iterating through the grid and calculating surface additions by examining each pair of adjacent cells directly.
This C solution calculates the surface area while accounting for shared walls between directly adjacent stacks of cubes. It inherently ensures overlap is addressed during iteration.
Time Complexity: O(n^2).
Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Direct Calculation of Surface Area | Time Complexity: O(n^2), where n is the dimension of the grid. Space Complexity: O(1), as no additional space proportional to the input size is used. |
| Iterative Side-by-Side Calculation | Time Complexity: O(n^2). Space Complexity: O(1). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Calculation of Surface Area | O(m*n) | O(1) | General case. Efficient single pass while subtracting shared faces between adjacent stacks. |
| Iterative Side-by-Side Calculation | O(m*n) | O(1) | Useful when reasoning about exposed sides from each direction rather than subtracting overlaps. |
Leetcode 892 - Arrays | Surface Area of 3D Shapes • Nideesh Terapalli • 3,085 views views
Watch 8 more video solutions →Practice Surface Area of 3D Shapes with our built-in code editor and test cases.
Practice on FleetCode