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

# Lucky Numbers in a Matrix Python Solution

> Tested Python solution for LeetCode 1380 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1380, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/lucky-numbers-in-a-matrix/description/).

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

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

## Problem

Given an `m x n` matrix of **distinct** numbers, return all **lucky numbers** in the matrix in **any** order.

A **lucky number** is an element of the matrix such that it is the minimum element in its row and maximum in its column.

### Examples

```
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
```

**Explanation:** 15 is the only lucky number since it is the minimum in its row and the maximum in its column.

```
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
```

**Explanation:** 12 is the only lucky number since it is the minimum in its row and the maximum in its column.

```
Input: matrix = [[7,8],[1,2]]
Output: [7]
```

### Constraints

* m == mat.length
* n == mat\[i].length
* 1 \<= n, m \<= 50
* 1 \<= matrix\[i]\[j] \<= 10^5
* All elements in the matrix are distinct.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(n)
    def lucky_numbers(self, matrix: list[list[int]]) -> list[int]:
        col_max = [max(col) for col in zip(*matrix, strict=True)]
        return [row_min for row in matrix if (row_min := min(row)) in col_max]
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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