Leetcode 1374 Solution

This article provides solution to leetcode question 1374 (leftmost-column-with-at-least-a-one)

https://leetcode.com/problems/leftmost-column-with-at-least-a-one

Solution

# """ # 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: m, n = binaryMatrix.dimensions()
ans = n for i in range(m): l = 0 r = min(ans, n - 1)
while l < r: m = (l + r) // 2
if binaryMatrix.get(i, m): r = m else: l = m + 1
if binaryMatrix.get(i, l): ans = min(ans, l)
return ans if ans != n else -1