LeetCode 1428. [Python] Leftmost Column with at Least a One
原题链接
中等
作者:
徐辰潇
,
2020-06-23 07:36:27
,
所有人可见
,
阅读 1120
class Solution:
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
#TC: max O(row + col)
#SC: O(1)
total_row, total_col = binaryMatrix.dimensions()[0], binaryMatrix.dimensions()[1]
row = total_row - 1
col = total_col - 1
res = total_col
while(row >= 0 and col >= 0):
if binaryMatrix.get(row, col) == 1:
res = min(res, col)
col -= 1
else:
row -= 1
if res == total_col:
return -1
else:
return res