> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Surface Area of 3D Shapes Python Solution

> Tested Python solution for LeetCode 892 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 892, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/surface-area-of-3d-shapes/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 892   # by problem number
lcpy gen -s surface_area_of_3d_shapes   # by problem name
```

## Problem

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg)

```
Input: grid = [[1,2],[3,4]]
Output: 34
```

![Example 2](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg)

```
Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 32
```

![Example 3](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg)

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/surface_area_of_3d_shapes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/surface_area_of_3d_shapes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) every cell inspected once with its four neighbours
    # Space: O(1)
    def surface_area(self, grid: list[list[int]]) -> int:
        size = len(grid)
        area = 0
        for row in range(size):
            for col in range(size):
                height = grid[row][col]
                if height == 0:
                    continue
                # Top and bottom faces are always exposed for a non-empty tower.
                area += 2
                # Four side faces per cube, minus what a neighbour hides.
                area += 4 * height
                for d_row, d_col in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                    n_row, n_col = row + d_row, col + d_col
                    if 0 <= n_row < size and 0 <= n_col < size:
                        area -= min(height, grid[n_row][n_col])
        return area
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) every cell inspected once with its four neighbours | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.