-
Notifications
You must be signed in to change notification settings - Fork 25
/
guessNumberHigherOrLower.py
37 lines (29 loc) · 1.06 KB
/
guessNumberHigherOrLower.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
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower,
# 1 if my number is higher, otherwise return 0
# this function is just added to pass the flake8 code linter
# delete this function definition while using it on leetcode
def guess(num: int) -> int:
pass
class Solution:
def guessNumber(self, n: int) -> int:
# here start = 1 ans end = n
# I supply mid as my pick and check with the original pick
# if pick is not right, update start or end accodingly
start = 1
end = n
while start <= end:
mid = start + (end - start) // 2
x = guess(mid)
if x == 0:
# element is found
return mid
elif x == -1:
# guessed element is greater than actual element
# search in the left of mid
end = mid - 1
else:
# guessed element is less than actual element
# search in the right of mid
start = mid + 1