Skip to main content

Surface Area of 3D Shapes - Solution & Explanation

EasyArrayMathGeometryMatrix13 min readAsked at: Amazon
Practice this problem

Problem Statement

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].length
  • 1 <= n <= 50
  • 0 <= grid[i][j] <= 50

Approach Overview

Problem 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.

Approach 1: Direct Calculation of Surface Area

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.

  1. Initialize the surface area as 0.
  2. Iterate through each position in the grid:
    • For each cell, add the surface area contributions of top and bottom faces.
    • For the side faces, subtract the area shared with adjacent cubes in the grid (up, down, left, right).
  3. Return the computed surface area.

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor β†’

Approach 2: Iterative Side-by-Side Calculation

This approach focuses on iterating through the grid and calculating surface additions by examining each pair of adjacent cells directly.

  • For each cell, compute initial assumption of no adjacent pipes.
  • Subtract common faces for vertical and horizontal neighbors.

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2).

Space Complexity: O(1).

Try this approach in the editor β†’

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
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β€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Calculation of Surface AreaO(m*n)O(1)General case. Efficient single pass while subtracting shared faces between adjacent stacks.
Iterative Side-by-Side CalculationO(m*n)O(1)Useful when reasoning about exposed sides from each direction rather than subtracting overlaps.

Video Solution

Leetcode 892 - Arrays | Surface Area of 3D Shapes β€’ Nideesh Terapalli β€’ 3,149 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Surface Area of 3D Shapes easy or hard?
Surface Area of 3D Shapes is categorized as an Easy problem on LeetCode with an acceptance rate above 70%. The challenge is recognizing how shared faces reduce surface area when stacks touch each other in the grid.
Surface Area of 3D Shapes Python/Java solution
Python and Java implementations follow the same idea: iterate over the grid, add the base contribution for each stack, and subtract overlapping faces with neighbors. The logic fits in a single nested loop and runs in O(m*n) time with constant space.
How to solve Surface Area of 3D Shapes in O(n)?
Treat the grid as a height map and scan each cell once. For every stack with height h, add its contribution and subtract shared faces with adjacent stacks using min(height1, height2). Since each cell is visited exactly once, the total runtime becomes O(m*n).
What is the best approach for Surface Area of 3D Shapes?
The best approach is the direct surface area calculation that subtracts shared faces between adjacent stacks. Iterate through the grid, add the base contribution of each stack (4*h + 2), and subtract overlaps with neighbors using min heights. This runs in O(m*n) time with O(1) extra space.
Is Surface Area of 3D Shapes asked at Google/Amazon/Meta?
Grid traversal and surface counting problems appear frequently in interviews at companies like Amazon and Google because they test spatial reasoning and matrix iteration. While this exact problem may vary, similar geometry-on-grid questions are common.
What data structure is used in Surface Area of 3D Shapes?
The primary data structure is a 2D array (matrix). Each element represents the height of stacked cubes at that position. The algorithm performs simple neighbor comparisons within the matrix.
What is the time complexity of Surface Area of 3D Shapes?
The time complexity is O(m*n), where m and n are the grid dimensions. Each cell is processed once, and only constant-time arithmetic and neighbor checks are performed. Space complexity remains O(1) because no additional data structures are required.

Ready to solve this problem?

Practice Surface Area of 3D Shapes with our built-in code editor and test cases.

Practice on FleetCode