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

# Brick Wall Python Solution with Tests

> Tested Python solution for LeetCode 554 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 554, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/brick-wall/description/).

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

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

## Problem

There is a rectangular brick wall in front of you with `n` rows of bricks. The i\<sup>th\</sup> row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.

Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.

Given the 2D integer array `wall` that contains the information about the wall, return *the minimum number of crossed bricks after drawing such a vertical line*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2025/01/17/a.png)

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

```
Input: wall = [[1],[1],[1]]
Output: 3
```

### Constraints

* n == wall.length
* 1 \<= n \<= 10^4
* 1 \<= wall\[i].length \<= 10^4
* 1 \<= sum(wall\[i].length) \<= 2 \* 10^4
* sum(wall\[i]) is the same for each row i.
* 1 \<= wall\[i]\[j] \<= 2^31 - 1

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n * m) where m is the max bricks per row
    # Space: O(n * m)
    def least_bricks(self, wall: list[list[int]]) -> int:
        edge_counts: Counter[int] = Counter()
        for row in wall:
            position = 0
            for width in row[:-1]:
                position += width
                edge_counts[position] += 1
        crossings = max(edge_counts.values(), default=0)
        return len(wall) - crossings
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m) where m is the max bricks per row | O(n \* m) |

## Tags

[NeetCode All](/catalog/neetcode).


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