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

# Leftmost Column with at Least a One

> Tested Python solution for LeetCode 1428 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1428, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Interactive](/catalog/topics/interactive), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/leftmost-column-with-one/description/).

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

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

## Problem

A \<strong>row-sorted binary matrix\</strong> means that all elements are \<code>0\</code> or \<code>1\</code> and each row of the matrix is sorted in non-decreasing order.

Given a \<strong>row-sorted binary matrix\</strong> \<code>binaryMatrix\</code>, return \<em>the index (0-indexed) of the \<strong>leftmost column\</strong> with a 1 in it\</em>. If such an index does not exist, return \<code>-1\</code>.

\<strong>You can't access the Binary Matrix directly.\</strong> You may only access the matrix using a \<code>BinaryMatrix\</code> interface:

\<ul>
\<li>\<code>BinaryMatrix.get(row, col)\</code> returns the element of the matrix at index \<code>(row, col)\</code> (0-indexed).\</li>
\<li>\<code>BinaryMatrix.dimensions()\</code> returns the dimensions of the matrix as a list of 2 elements \<code>\[rows, cols]\</code>, which means the matrix is \<code>rows x cols\</code>.\</li>
\</ul>

Submissions making more than \<code>1000\</code> calls to \<code>BinaryMatrix.get\</code> will be judged \<em>Wrong Answer\</em>.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1428.Leftmost%20Column%20with%20at%20Least%20a%20One/images/untitled-diagram-5.jpg)

```
Input: mat = [[0,0],[1,1]]
Output: 0
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1428.Leftmost%20Column%20with%20at%20Least%20a%20One/images/untitled-diagram-4.jpg)

```
Input: mat = [[0,0],[0,1]]
Output: 1
```

![Example 3](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1428.Leftmost%20Column%20with%20at%20Least%20a%20One/images/untitled-diagram-3.jpg)

```
Input: mat = [[0,0],[0,0]]
Output: -1
```

### Constraints

* \<code>rows == mat.length\</code>
* \<code>cols == mat\[i].length\</code>
* \<code>1 \<= rows, cols \<= 100\</code>
* \<code>mat\[i]\[j]\</code> is either \<code>0\</code> or \<code>1\</code>.
* \<code>mat\[i]\</code> is sorted in non-decreasing order.

\<strong>Follow up:\</strong> Could you find a solution with a complexity better than \<code>O(rows x cols)\</code>?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class BinaryMatrix:
    # Test-harness API: backs the interactive get/dimensions interface with the matrix
    def __init__(self, mat: list[list[int]]) -> None:
        self.mat = mat
        self.calls = 0

    def get(self, row: int, col: int) -> int:
        self.calls += 1
        return self.mat[row][col]

    def dimensions(self) -> list[int]:
        return [len(self.mat), len(self.mat[0])]


class Solution:
    # Time: O(rows + cols)
    # Space: O(1)
    def leftmost_column_with_one(self, binary_matrix: BinaryMatrix) -> int:
        rows, cols = binary_matrix.dimensions()
        row, col = 0, cols - 1
        result = -1
        while row < rows and col >= 0:
            if binary_matrix.get(row, col) == 1:
                result = col
                col -= 1
            else:
                row += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(rows + cols) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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