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

# Smallest Rectangle Enclosing Black Pixels

> Tested Python solution for LeetCode 302 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 302, [Hard](/catalog/hard). Topics: [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/description/).

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

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

## Problem

You are given an `m x n` binary matrix `image` where `0` represents a white pixel and `1` represents a black pixel.

The black pixels are connected (i.e., there is only one black region). Pixels are connected horizontally and vertically.

Given two integers `x` and `y` that represents the location of one of the black pixels, return *the area of the smallest (axis-aligned) rectangle that encloses all black pixels*.

You must write an algorithm with less than `O(mn)` runtime complexity.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0302.Smallest%20Rectangle%20Enclosing%20Black%20Pixels/images/pixel-grid.jpg)

```
Input: image = [["0010"],["0110"],["0100"]], x = 0, y = 2
Output: 6
```

```
Input: image = [["1"]], x = 0, y = 0
Output: 1
```

### Constraints

* `m == image.length`
* `n == image[i].length`
* `1 <= m, n <= 100`
* `image[i][j]` is either `'0'` or `'1'`.
* `0 <= x < m`
* `0 <= y < n`
* `image[x][y] == '1'`.
* The black pixels in the `image` only form **one component**.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m log m + n log n) — binary search for each bounding edge
    # Space: O(1)
    def min_area(self, image: list[list[str]], x: int, y: int) -> int:
        m, n = len(image), len(image[0])

        def row_has_black(r: int) -> bool:
            return "1" in image[r]

        def col_has_black(c: int) -> bool:
            return any(row[c] == "1" for row in image)

        lo, hi = 0, x
        while lo < hi:
            mid = (lo + hi) // 2
            if row_has_black(mid):
                hi = mid
            else:
                lo = mid + 1
        top = lo

        lo, hi = x, m - 1
        while lo < hi:
            mid = (lo + hi + 1) // 2
            if row_has_black(mid):
                lo = mid
            else:
                hi = mid - 1
        bottom = lo

        lo, hi = 0, y
        while lo < hi:
            mid = (lo + hi) // 2
            if col_has_black(mid):
                hi = mid
            else:
                lo = mid + 1
        left = lo

        lo, hi = y, n - 1
        while lo < hi:
            mid = (lo + hi + 1) // 2
            if col_has_black(mid):
                lo = mid
            else:
                hi = mid - 1
        right = lo

        return (bottom - top + 1) * (right - left + 1)
```

## Complexity

| Time | Space |
| - | - |
| O(m log m + n log n) — binary search for each bounding edge | O(1) |

## Tags


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