๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/LEETCODE

Search in Rotated Sorted Array | LeetCode 804 | Python3 ๐Ÿ

๋ฐ˜์‘ํ˜•

 

๐Ÿ“„ ๋ชฉ์ฐจ

     

     

    ๐Ÿค” ๋ฌธ์ œ : Search in Rotated Sorted Array | LeetCode 804

    ๋ฌธ์ œ: https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/804/

    ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ํƒ€๊ฒŸ ์ˆซ์ž์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

    ๋‹จ, ์—ฌ๊ธฐ์„œ ๋ฐฐ์—ด์€ rotated ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ๋“ค์–ด,

    [0,1,2,4,5,6,7]

    ๋ฐฐ์—ด์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 4์นธ ์˜ฎ๊ฒจ 

    [4,5,6,7,0,1,2]

    ๋ฅผ ๋งŒ๋“œ๋Š” ์‹์ž…๋‹ˆ๋‹ค.

     

     

    ๐Ÿ’ก ํ’€์ด

    1. Rotate ๋œ ์‹œ์ž‘์ ์„ ์ฐพ์€ ํ›„ ํ•œ์ชฝ ๋ฒ”์œ„์—์„œ BS

    ๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ฌ๋ฆฐ ํ’€์ด๋Š” ์–ด๋Š๋ถ€๋ถ„๋ถ€ํ„ฐ Rotate๋œ ๊ฒƒ์ธ์ง€, pivot์„ ์ฐพ์ž๋Š” ๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค.

    nums = [4,5,6,7,0,1,2], target = 0

    ์ด ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ 7๊ณผ 0 ์‚ฌ์ด๊ฐ€ rotate ๋œ ๊ฑฐ๊ฒ ์ฃ ? 

    ๊ทธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ ๋‘˜๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. 

    1) [ 4, 5, 6, 7 ]

    2) [ 0, 1, 2 ]

     

    ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์€ ์ž˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์ด๋‹ˆ Binary Search๋ฅผ ์“ธ ์ˆ˜ ์žˆ์ฃ . 

    ๋‘˜ ์ค‘ ํƒ€๊ฒŸ์„ ํฌํ•จํ•˜๋Š” ๋ฒ”์œ„์ธ 2) ๋ฐฐ์—ด์— ๋Œ€ํ•ด์„œ Binary Search๋ฅผ ์ ์šฉํ•˜์—ฌ ํƒ€๊ฒŸ๊ฐ’ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.  

     

    class Solution:
        def search(self, nums: List[int], target: int) -> int:
            if not nums: return -1
            if len(nums) == 1: return 0 if target == nums[0] else -1
            def bsPivot(st, ed):  # rotated ์ง€์ ์„ ์ฐพ๊ธฐ ์œ„ํ•œ Binary Search 
                md = int((st+ed)/2)
                if nums[md] > nums[md+1] : return md
                if nums[md] >= nums[st]: return bsPivot(md+1, ed)
                return bsPivot(st, md-1)
            def bs(st, ed):  # ํƒ€๊ฒŸ์„ ์ฐพ๊ธฐ ์œ„ํ•œ Binary Search
                # print("bs", st, ed)
                if st > ed: return -1
                md = int((st+ed)/2)
                if nums[md] == target: return md
                if nums[md] > target: return bs(st, md-1)
                return bs(md+1, ed)
            pivot = len(nums)-1 if nums[-1] > nums[0] else bsPivot(0, len(nums)-1)
            # print(pivot)
            if nums[0] <= target: return bs(0, pivot)
            else: return bs(pivot+1, len(nums)-1)

     

    Time Complexity
    logN(pivot์ฐพ๊ธฐ) + logN
    O(logN)

     

    ์„ฑ๋Šฅ์ด ๋ฌด์ฒ™ ๋ณ„๋กœ๋„ค์š”.. ์•„๋ฌด๋ž˜๋„ ๋” ์ข‹์€ ํ’€์ด๊ฐ€ ์žˆ๋‚˜๋ด…๋‹ˆ๋‹ค.

     

    2. BS๋ฅผ ๋ฐ”๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ?

    ์ฒซ๋ฒˆ์งธ ํ’€์ด๋Š” ์•„๋ฌด๋ž˜๋„ ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ rotate ์ง€์ ์„ ์ฐพ์•„์•ผ ํ•˜๋‹ˆ n์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

    (rorate์ง€์ ์„ binary search๋กœ ์ฐพ์„์ˆ˜๋„ ์žˆ์ง€๋งŒ.. ์˜คํžˆ๋ ค ์—ฐ์‚ฐ์ด ๋ณต์žกํ•˜์—ฌ ๋” ๋Š๋ฆฌ๋”๋ผ๊ตฌ์š”)

     

    rotate์ง€์ ์„ ์ฐพ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ BS๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์ด ์žˆ์„๊นŒ์š”?

     

    BS์˜ ํŠน์ง•์€ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฐ˜์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ํƒ€๊ฒŸ๊ฐ’์„ ์ฐพ๋Š”๊ฒƒ์ž…๋‹ˆ๋‹ค.

    rotated ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ๋ฒ”์œ„๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ  ๋ฐฐ์—ด ๋‘๊ฐœ๋กœ ๋งŒ๋“ค๋ฉด, 

    1) ์ž˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด -  ์ฒซ๊ฐ’์ด ๋งˆ์ง€๋ง‰๊ฐ’๋ณด๋‹ค ์ž‘์Œ

    2) rotated ๋ฐฐ์—ด - ์ฒซ ๊ฐ’์ด ๋งˆ์ง€๋ง‰๊ฐ’๋ณด๋‹ค ํผ

    ์ด๋ ‡๊ฒŒ ๋‚˜๋ˆ ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

    [ 4, 5, 6, 7, 0, 1, 2 ]
    [ 4, 5, 6] [7, 0, 1, 2]
         X     [7, 0][1, 2]
                  0     X

    1) ๋ฐฐ์—ด์—์„œ ํƒ€๊ฒŸ ๋ฒ”์œ„๋ฅผ ํฌํ•จํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š”์ง€๋Š” ๊ฐ„๋‹จํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ฃ ?

    1) ๋ฐฐ์—ด์ด ํƒ€๊ฒŸ ๋ฒ”์œ„๋ฅผ ํฌํ•จํ•œ๋‹ค๋ฉด 1)์— ๋Œ€ํ•ด BS๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ 

    ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด 2)์— ๋Œ€ํ•ด BS๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ๋˜๊ฒ ๋„ค์š”. 

     

    class Solution:
        def search(self, nums: List[int], target: int) -> int:
            if not nums: return -1
            if len(nums) == 1: return 0 if target == nums[0] else -1
            def bs(st, ed):
                # print(st, ed)
                if st > ed: return -1
    
                md = int((st+ed)/2)
                if nums[md] == target: return md
    
                # ์™ผ์ชฝ์€ ๋ฐ”๋ฅด๊ฒŒ ์ •๋ ฌ๋จ
                if nums[md] >= nums[st]:
                    if nums[st] <= target <= nums[md]: return bs(st, md-1)
                    else: return bs(md+1, ed)
                # ์˜ค๋ฅธ์ชฝ์€ ๋ฐ”๋ฅด๊ฒŒ ์ •๋ ฌ๋จ
                else:
                    if nums[md] <= target <= nums[ed]: return bs(md+1, ed)
                    else: return bs(st, md-1)
    
            return bs(0, len(nums)-1)

     

    Time Complexity
    O(logN)

     

    ๋ฐ˜์‘ํ˜•