Skip to main content
LeetCode 1428, Medium. Topics: Array, Binary Search, Interactive, Matrix. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

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
Example 2
Example 3

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026