Surface Area of 3D Shapes — Easy Problem & Solution

An n x n board is covered with stacks of unit cubes: grid[i][j] cubes are stacked on cell (i, j).

  • Difficulty: Easy
  • Topics: Arrays, Math, Matrix, Geometry
  • Asked at: Google, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

An n x n board is covered with stacks of unit cubes: grid[i][j] cubes are stacked on cell (i, j). Cubes that touch each other — in the same stack or in neighbouring stacks — are glued together, so the whole thing may form one or more solid shapes.

Return the total surface area of the resulting shapes. The bottom faces resting on the board count as surface too.

Example 1

Input: grid = [[2]]
Output: 10
Explanation: One stack of two cubes: four sides of area 2, plus top and bottom.

Example 2

Input: grid = [[1,0],[0,2]]
Output: 16
Explanation: The stacks touch only at an edge, so they are counted separately: 6 + 10.

Example 3

Input: grid = [[3,3],[3,3]]
Output: 32
Explanation: A 2 x 2 x 3 box: 2·(2·2 + 2·3 + 2·3) = 32.

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 50
  • 0 <= grid[i][j] <= 50

How to solve Surface Area of 3D Shapes

Count every stack as if it stood alone, then subtract the faces that disappear where neighbouring stacks are glued together.

Approach

  1. For each cell with v > 0, add 4v + 2 (four walls of height v, a top and a bottom).
  2. For each cell, look at its right neighbour and its lower neighbour (so each adjacent pair is seen once) and subtract 2 · min(v, neighbour).
  3. Return the total.

Why it works

Within one stack, the glued faces between cubes are already excluded by 4v + 2. Between two adjacent stacks of heights a and b, the cubes at levels 0 .. min(a, b) - 1 face each other, and each such pair hides one face from each side — 2·min(a, b) faces. Diagonal stacks touch only along an edge and hide nothing. Every hidden face belongs to exactly one adjacent pair, and visiting only right and down neighbours counts each pair once.

Complexity

  • Time — O(n²)
  • Space — O(1)

Pitfalls

  • An empty cell must not contribute its +2 for top and bottom.
  • Subtract twice the shared height, not once — both stacks lose a face.
  • Visiting all four neighbours of every cell double-counts each pair unless you halve the result.

Reference solution

Python

from typing import List

def surfaceArea(grid: List[List[int]]) -> int:
    n = len(grid)
    total = 0
    for i in range(n):
        for j in range(n):
            v = grid[i][j]
            if v > 0:
                total += 4 * v + 2
            if i + 1 < n:
                total -= 2 * min(v, grid[i + 1][j])
            if j + 1 < n:
                total -= 2 * min(v, grid[i][j + 1])
    return total

JavaScript

var surfaceArea = function(grid) {
    var n = grid.length;
    var total = 0;
    for (var i = 0; i < n; i++) {
        for (var j = 0; j < n; j++) {
            var v = grid[i][j];
            if (v > 0) total += 4 * v + 2;
            if (i + 1 < n) total -= 2 * Math.min(v, grid[i + 1][j]);
            if (j + 1 < n) total -= 2 * Math.min(v, grid[i][j + 1]);
        }
    }
    return total;
};

Also on the editorial tab: C, C#, C++, Go, Java, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 988 arrays problems · the whole catalogue

Learn the technique: Arrays · Matrix and Grid Traversal