만족은 하되 안주하지는 말자

기록해야 기억한다

프로그래밍/leetcode

[leetcode] 81. Search in Rotated Sorted Array II

D36choi 2022. 3. 28. 20:22
728x90

문제

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

 

Search in Rotated Sorted Array II - 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

내 풀이

from collections import deque
class Solution:
    def search(self, nums: List[int], target: int) -> bool:
        nums = deque(set(nums))
        last = nums[-1]
        while nums:
            if nums[0] <= last:
                break
            
            nums.rotate(-1)
            
        
        
        left,right = 0, len(nums) - 1
        while(left <= right):
            pivot = left + (right - left) // 2
            if nums[pivot] == target:
                return True
            elif nums[pivot] < target:
                left = pivot + 1
            else:
                right = pivot - 1
        
        
        return False

알게 된 것

deque().rotate(-1) : deque의 원소를 -1만큼 회전한다.

binary search 의 python version을 외운듯 하다

 

피드백