File: surface-area-of-3d-shapes/solution.py

Date: 2026-06-06

Time: 19:24

Purpose

This file implements LeetCode 892: Surface Area of 3D Shapes. It solves the problem of computing the total exposed surface area of a 3D structure formed by stacking unit cubes on an n x n grid, where grid[i][j] specifies the height (number of cubes) at position (i, j). The file is self-contained — solution class and test suite in one module.

Key Components

Solution.surfaceArea(grid: List[List[int]]) -> int

The core algorithm. It computes exposed surface area by:

1. Adding the full surface of each column of cubes in isolation: 2 + 4 * v (2 for top/bottom caps, 4 sides per cube in the column).

2. Subtracting hidden faces between adjacent columns. For each pair of neighbors, 2 * min(v, neighbor) faces are mutually occluded (one face on each side of the contact).

The subtraction only checks the right and down neighbors (i+1, j+1), avoiding double-counting — each adjacency is visited exactly once.

TestSurfaceArea

Eight test cases covering the three LeetCode examples, edge cases (single cell with 0, 1, and 5 cubes), an all-zero grid, and a uniform-height grid. The uniform-height test includes a manual verification comment: 9 cells × 6 faces = 54, minus 24 shared faces (12 adjacencies × 2) = 30.

Patterns

Dependencies

Imports: typing.List (type annotation), unittest (test framework). No project-internal dependencies.

Imported by: The test_solution.py file in this same directory, plus the "Imported By" list in the prompt is misleading — that list appears to be a cross-repo artifact from test scaffolding that imports a shared base, not direct imports of this specific solution.

Flow

For a grid like [[1, 2], [3, 4]]:

1. Cell (0,0), v=1: add 2 + 4 = 6. Right neighbor is 2 → subtract 2*min(1,2) = 2. Down neighbor is 3 → subtract 2*min(1,3) = 2. Running: 2.

2. Cell (0,1), v=2: add 2 + 8 = 10. No right neighbor. Down neighbor is 4 → subtract 2*min(2,4) = 4. Running: 8.

3. Cell (1,0), v=3: add 2 + 12 = 14. Right neighbor is 4 → subtract 2*min(3,4) = 6. No down neighbor. Running: 16.

4. Cell (1,1), v=4: add 2 + 16 = 18. No neighbors to subtract. Final: 34.

Invariants

Error Handling

None. The function assumes a valid n x n grid with non-negative integers, consistent with LeetCode's constraints. No bounds checking or input validation.

Topics to Explore

Beliefs