728x90
while 문으로 해결하려 했더니 시간초과
Binary Search로 풀어야 함.
# 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
# def guess(num: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
left, right = 1, n
while left <= right:
mid = (right + left) // 2
if guess(mid) <= 0: # noqa
right = mid - 1
else:
left = mid + 1
return left
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] First Unique Character in a String 파이썬 (Counter, str.find) (0) | 2021.04.14 |
---|---|
[LeetCode] Ransom Note 파이썬 (dict, set, count) (0) | 2021.04.14 |
[LeetCode] Valid Perfect Square 파이썬 (Binary Search, +Lower Bound) (0) | 2021.04.14 |
[LeetCode] Intersection of Two Arrays II 파이썬 (defaultdict, Counter, extend) (0) | 2021.04.13 |
[LeetCode] Intersection of Two Arrays 파이썬 (set, intersection) (0) | 2021.04.13 |
댓글