Published:
Updated:

  • ์ด์ง„ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์šฉ
  • target๋ณด๋‹ค ์ค‘๊ฐ„ ๊ฐ’์ด ์ž‘์œผ๋ฉด ์ฆ๊ฐ€์‹œ์ผœ๊ฐ€์•ผ ํ•˜๊ณ 
  • ํฌ๋ฉด ๊ฐ์†Œ์‹œ์ผœ๊ฐ€์•ผ ํ•˜๊ณ 
  • ๊ฐ™์œผ๋ฉด ๊ทธ mid ๊ฐ’์ด index ๊ฐ’์ด๋‹ˆ ๋ฆฌํ„ดํ•˜๋ฉด ๋จ


Solution

from typing import List


class Solution:
    # O(log n)์œผ๋กœ ํ’€๊ธฐ
    def search(self, nums: List[int], target: int) -> int:
        lt, rt = 0, len(nums) - 1

        while lt <= rt:
            mid = (lt + rt) // 2

            if nums[mid] == target:
                return mid

            if nums[mid] < target:
                mid += lt
                lt += 1
                continue

            if nums[mid] > target:
                mid -= rt
                rt -= 1

        return -1


Reference

Leave a comment