374. Guess Number Higher or Lower
# 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:
l, r = 1, n
while l<=r:
mid = (l+r)//2
if guess(mid) == 1: # num < pick
l = mid+1
elif guess(mid) == -1: # num > pick
r = mid-1
else: # num == pick
return mid
https://youtu.be/xW4QsTtaCa4?si=Aoi71z3JICWHxs0a
'LeetCode > LeetCode75' 카테고리의 다른 글
BST: 875. Koko Eating Bananas ★ (0) | 2024.11.22 |
---|---|
[LeetCode 75] Medium - 2300. Successful Pairs of Spells and Potions (0) | 2024.11.22 |
[LeetCode 75] Medium - 2462. Total Cost to Hire K Workers (1) | 2024.11.19 |
[LeetCode 75] Medium - 2542. Maximum Subsequence Score (0) | 2024.11.19 |
[LeetCode 75] Medium - 1926. Nearest Exit from Entrance in Maze (3) | 2024.11.19 |