Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 11. 16. 23:45

https://leetcode.com/problems/guess-number-higher-or-lower/

 

Guess Number Higher or Lower - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

야근이라 패스하고 싶었으나....... 스트릭 유지를 위해 그냥 함
딱봐도 바이너리 서치(이분 탐색) 문제인데, 구현을 제대로 못했음 ㅠㅠ
다음부턴 low/high로 나누는 걸 시작점으로 보고 풀어야겠다!

class Solution:
    def guessNumber(self, n: int) -> int:
        l, h = 1, n        
        while l <= h:            
            mid = l + (h - l) // 2
            res = guess(mid)
            if res == 0:
                return mid
            if res < 0:
                h = mid - 1
                continue
            if res > 0:
                l = mid + 1
        return -1
반응형