Leftmost column with at least a one

December 29, 2022

binary-search

Problem URL: Leftmost column with at least a one

We do a binary search on each of the $m$ rows, finding the earliest 1 on each. On this binary search, we only search the first minCol columns, where minCol is the earliest column where we have found a 1 so far; at first we set minCol = n.

# """
# This is BinaryMatrix's API interface.
# You should not implement it, or speculate about its implementation
# """
#class BinaryMatrix(object):
#    def get(self, row: int, col: int) -> int:
#    def dimensions(self) -> list[]:

class Solution:
    def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
        rows, cols = binaryMatrix.dimensions()
        res = cols
        for r in range(rows):
            low, high = 0, cols
            while low < high:
                mid = (low + high) // 2
                if binaryMatrix.get(r, mid) < 1:
                    low = mid + 1
                else:    
                    high = mid
            res = min(res, low)        
        return res if res < cols else -1 

Time complexity: O(mlog(n))
Space complexity: O(1)