Skip to main content
Back to problems
#1428
Medium Algorithms

Leftmost column with at least a one

Array Binary Search Matrix Interactive
55.1% acceptance
Mar 31, 2026
1257
153
A row-sorted binary matrix means that all elements are 0 or 1 and each row of the matrix is sorted in non-decreasing order. Given a row-sorted binary matrix binaryMatrix, return the index (0-indexed) of the leftmost column with a 1 in it. If such an index does not exist, return -1. You can't access the Binary Matrix directly. You may only access the matrix using a BinaryMatrix interface: BinaryMatrix.get(row, col) returns the element of the matrix at index (row, col) (0-indexed). BinaryMatrix.dimensions() returns the dimensions of the matrix as a list of 2 elements [rows, cols], which means the matrix is rows x cols. Submissions making more than 1000 calls to BinaryMatrix.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification. For custom testing purposes, the input will be the entire binary matrix mat. You will not have access to the binary matrix directly.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
/**
 * // This is the BinaryMatrix's API interface.
 * // You should not implement it, or speculate about its implementation
 *  struct BinaryMatrix;
 *  impl BinaryMatrix {
 *      fn get(&self, row: i32, col: i32) -> i32;
 *     fn dimensions() -> Vec<i32>;
 * };
 */

impl Solution {
  pub fn left_most_column_with_one(binary_matrix: &BinaryMatrix) -> i32 {
    let dims = binary_matrix.dimensions();
    let (rows, cols) = (dims[0], dims[1]);
    let mut r = 0;
    let mut c = cols - 1;
    let mut ans = -1;
    while r < rows && c >= 0 {
      if binary_matrix.get(r, c) == 1 {
        ans = c;
        c -= 1;
      } else {
        r += 1;
      }
    }
    ans
  }
}