Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions leetcode3/변지협/v3/374. Guess Number Higher or Lower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
1. 아이디어:
이진탐색으로 풀이.
2. 시간복잡도:
o(log n)
3. 알고리즘:
이진탐색
'''

# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
# 1 if num is lower than the picked number
# otherwise return 0
# def guess(num: int) -> int:

class Solution:
def guessNumber(self, n: int) -> int:
current = n // 2 + 1

# while True:
while True:
# print(current)
g = guess(current)

if g == -1:
n = current
current = n // 2
elif g == 1:
current = (n + current + 1) // 2
else:
return current