1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int: rows, cols = binaryMatrix.dimensions()
def find_one_columnwise(col): if col < 0: return -1 for i in range(rows): if binaryMatrix.get(i, col) == 1: return i return -1
def binary_search(row, col): left, right = 0, col while left < right: mid = (left+right)>>1 if binaryMatrix.get(row,mid) == 0: left = mid + 1 else: right = mid
return left
col = cols -1 while (r := find_one_columnwise(col)) >= 0: col = binary_search(r, col) -1
return -1 if col == cols -1 else col+1
|