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

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

Sort Colors | LeetCode 798 | Python3 ๐Ÿ

๋ฐ˜์‘ํ˜•

 

๐Ÿ“„ ๋ชฉ์ฐจ

     

     

     

    ๐Ÿค” ๋ฌธ์ œ : Longest Palindromic Substring | LeetCode 780

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

    ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

    ๋‹จ, ์ด ๋ฌธ์ œ์—์„œ ๋ฐฐ์—ด์˜ ๊ฐ’์€ 0,1,2์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 

    quick sort๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋ฉด O(NlogN) ์ด ๊ฑธ๋ฆฌ๊ฒ ์ง€๋งŒ, ๋ฐฐ์—ด์˜ ๊ฐ’์ด ์ •ํ•ด์ ธ์žˆ๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•˜์—ฌ ๋” ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๐Ÿ’ก ํ’€์ด

    1.  hashMapํ™œ์šฉ, 0,1,2์˜ ๊ฐฏ์ˆ˜ count

    ๋ฐฐ์—ด์—์„œ ๋‚˜์˜ค๋Š” ๊ฐ’์„ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ํ™œ์šฉํ•ด๋ด…๋‹ˆ๋‹ค.

    nums๋ฅผ ์ฝ์œผ๋ฉฐ 0,1,2์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ ,

    ํ•œ๋ฒˆ ๋” ์ˆœํšŒํ•˜๋ฉฐ ๊ฐฏ์ˆ˜๋Œ€๋กœ ์„ธํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

    class Solution:
        def sortColors(self, nums: List[int]) -> None:
            counts = {0: 0, 1: 0, 2: 0}
            for item in nums:
                counts[item] += 1
    
            index = 0
            for i in range(3):
                while counts[i] > 0:
                    nums[index] = i
                    index += 1
                    counts[i] -= 1โ€‹

     

    ๋ฐฐ์—ด์„ ์ด 2๋ฒˆ ์ˆœํšŒํ•˜๊ณ , hashmap ํฌ๊ธฐ๋งŒํผ์˜ ์ €์žฅ๊ณต๊ฐ„์„ ๋” ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 

    Time Complexity
    O(N)

    Space Complexity
    O(1)

     

    2.  one-pass algorithm

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

    Follow up: Could you come up with a one-pass algorithm using only constant extra space?

     
    ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฉด์„œ ์ •๋ ฌ์„ ํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋Š” ์–˜๊ธฐ๊ฒ ์ฃ ?
     
    ๋ฐฐ์—ด์— ์žˆ๋Š” ์›์†Œ๊ฐ€ ์„ธ์ข…๋ฅ˜ ๋ฐ–์— ์—†๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ’€์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
    • 0์„ ์•ž์—์„œ๋ถ€ํ„ฐ ์ฑ„์›Œ๋‚˜๊ฐ€๋ฉฐ, 0์ด ์ฑ„์›Œ์ง„ ์ธ๋ฑ์Šค ๊ธฐ๋ก (frontIdx)
    • 0์ด ๋‚˜์˜ค๋ฉด frontIdx์˜ ๊ฐ’๊ณผ swap
    • 2์„ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฑ„์›Œ๋‚˜๊ฐ€๋ฉฐ, 2๊ฐ€ ์ฑ„์›Œ์ง„ ์ธ๋ฑ์Šค ๊ธฐ๋ก (backIdx)
    • 2๊ฐ€ ๋‚˜์˜ค๋ฉด backIdx ๊ฐ’๊ณผ swap
    class Solution:
        def sortColors(self, nums: List[int]) -> None:
            backIdx = len(nums) - 1
            frontIdx = 0
            currIdx = 0
            while 1:
                if currIdx > backIdx: break
                if nums[currIdx] is 0:
                    nums[frontIdx], nums[currIdx] = nums[currIdx], nums[frontIdx]
                    frontIdx += 1
                if nums[currIdx] is 2:
                    nums[backIdx], nums[currIdx] = nums[currIdx], nums[backIdx]
                    backIdx -= 1
                    currIdx -= 1 	[1] ์ฃผ์˜!
                currIdx += 1
    
            print(nums)

    [1] 2๋ฅผ swapํ•˜๊ณ  ๋‚˜๋ฉด currIdx์ž๋ฆฌ์˜ ๊ฐ’์— ๋ญ๊ฐ€ ๋“ค์–ด์˜ฌ ์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•ด๋‹น ๊ฐ’์€ ๋‹ค์‹œ ๋ด์ฃผ๊ธฐ ์œ„ํ•ด -1ํ•ด์ค๋‹ˆ๋‹ค.

     

    time complexity๋Š” ๋™์ผํ•˜๊ฒŒ O(N)์ด์ง€๋งŒ, ์ „์ฒด ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ˆ˜ํ–‰์†๋„๋Š” ์กฐ๊ธˆ ๋” ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค.

     

    3.  2๋ฒˆ ์‚ด์ง ๊ฐœ์„  ,, 

    2๋ฒˆ ํ’€์ด์—์„œ 2๊ฐ€์ง€๋ฅผ ๊ฐœ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

    • nums์˜ ์•ž๋ถ€๋ถ„์ด 0์ธ ๊ฒฝ์šฐ skip
    • nums์˜ ๋’ท๋ถ€๋ถ„์ด 1์ธ ๊ฒฝ์šฐ skip
    • 0๊ณผ 0์„ swapํ•˜๋Š” ๊ฒฝ์šฐ skip
    class Solution:
        def sortColors(self, nums: List[int]) -> None:
            if not nums: return nums
            backIdx = len(nums) - 1
            frontIdx = 0
            currIdx = 0
            while frontIdx <= backIdx and nums[frontIdx] is 0 : frontIdx += 1
            while frontIdx <= backIdx and nums[backIdx] is 2:  backIdx -= 1
            currIdx = frontIdx
            while currIdx <= backIdx:
                if nums[currIdx] is 0:
                    if currIdx is not frontIdx:
                        nums[frontIdx], nums[currIdx] = nums[currIdx], nums[frontIdx]
                    frontIdx += 1
                if nums[currIdx] is 2:
                    nums[backIdx], nums[currIdx] = nums[currIdx], nums[backIdx]
                    backIdx -= 1
                    currIdx -= 1
                currIdx += 1

     

    ์›Œ๋‚™ ํ’€์ด๋“ค๋ผ๋ฆฌ ์ˆ˜ํ–‰์†๋„  ์ฐจ์ด๊ฐ€ ๋ฏธ๋ฏธํ•ด์„œ์ธ์ง€, ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟจ๋”๋‹ˆ 93%๋กœ ์˜ฌ๋ผ๊ฐ”๋„ค์š”.

    ๋ฐ˜์‘ํ˜•