-
Notifications
You must be signed in to change notification settings - Fork 0
/
21_leftmost_column_with_at_least_a_one.py
51 lines (47 loc) · 1.51 KB
/
21_leftmost_column_with_at_least_a_one.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#contest url:https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3306/
# """
# This is BinaryMatrix's API interface.
# You should not implement it, or speculate about its implementation
# """
#class BinaryMatrix(object):
# def get(self, x: int, y: int) -> int:
# def dimensions(self) -> list[]:
#thanks, discussions
class Solution:
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
row, col = binaryMatrix.dimensions()
result = -1
for i in range(row):
start = 0
end = col -1
while start < end:
mid = start + (end - start) // 2
if binaryMatrix.get(i, mid) == 0:
start = mid+1
else:
end = mid
if(binaryMatrix.get(i, end) != 0):
result = (end if result == -1 else min (result, end))
return result
# found = 0
# start = 0
# end = col
# while start <= end:
# mid = start+(end-start)//2
# i = 0
# flag = 0
# while i < row:
# if binaryMatrix.get(i, mid):
# flag = 1
# break
# else:
# i+=1
# if flag == 0:
# start = mid+1
# else:
# end = mid
# found = 1
# if found:
# return start
# else:
# return -1