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

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

Permutations, Subsets | LeetCode | Python3 ๐Ÿ

๋ฐ˜์‘ํ˜•

 

๐Ÿ“„ ๋ชฉ์ฐจ

     

     

     

    ๋ฐฑํŠธ๋ ˆํ‚น์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ์ธ permutation๊ณผ subset์„ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    ๐Ÿค” ๋ฌธ์ œ1 : Permutations

    ๋ฌธ์ œ: https://leetcode.com/problems/permutations/

    ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ๋กœ ์ˆœ์„œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์„ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. 

    class Solution:
        def subsets(self, nums: List[int]) -> List[List[int]]:
            ret = []
            def getPermutation(current_array, item_list):
                ret.append(current_array)
                if not item_list: return
                for idx in range(len(item_list)):
                    getPermutation(current_array+[item_list[idx]], (item_list[idx+1:]))
    
            getPermutation([], nums)
            return ret

     

    ๐Ÿ’ก ํ’€์ด

    1. Backtracking

    from typing import List
    
    class Solution:
        def permute(self, nums: List[int]) -> List[List[int]]:
            ret = []
            def getPermutation(current_array, item_list):
                if not item_list: ret.append(current_array)
                for idx in range(len(item_list)):
                    getPermutation(current_array+[item_list[idx]], (item_list[:idx] if idx != 0 else [])+(item_list[idx+1:]))
    
            getPermutation([], nums)
            return ret

    current_array ์—๋Š” ํ˜„์žฌ๊นŒ์ง€ ๋งŒ๋“  ๋ฆฌ์ŠคํŠธ๋ฅผ, item_list์—๋Š” ์•ž์œผ๋กœ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์•ผ ํ•  ์•„์ดํ…œ๋“ค์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. 

     

    ๋‚ด๋ถ€์—์„œ getPermutation์„ ๊ณ„์† ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ์กฐ์ด๊ณ , getPermutation ์ž์ฒด๊ฐ€ ํ•˜๋‚˜์˜ for loop์„ ๊ฐ–๊ณ  ์žˆ์–ด์„œ

    ์ „์ฒด ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ ๋‹ค ํ•œ๋ฒˆ์”ฉ ๋ณด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n!)์ด๊ฒ ๋„ค์š”

     

     

    ๐Ÿค” ๋ฌธ์ œ2 : Subsets

    ๋ฌธ์ œ: https://leetcode.com/problems/subsets/

    ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

    ์ ‘๊ทผ๋ฐฉ์‹์€  Permutation๊ณผ์œ ์‚ฌํ•˜์ง€๋งŒ 1. ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , 2. ๋ฆฌ์ŠคํŠธ์˜ ์•„์ดํ…œ์ด n๊ฐœ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค. 

     

     

    ๐Ÿ’ก ํ’€์ด

    1. Permutations ๊ธฐ๋ฐ˜์˜ ํ’€์ด

    ์ฒ˜์Œ์—๋Š” Permutations ๋ฌธ์ œ์—์„œ ์กฐ๊ธˆ๋งŒ ๋ณ€ํ˜•ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

    ๋ชจ๋“  ์•„์ดํ…œ์„ ๋‹ค ๋„ฃ์„ ํ•„์š”๋Š” ์—†์œผ๋‹ˆ item_list์— ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ์•„์ดํ…œ์€ ํฌํ•จํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

    class Solution:
        def subsets(self, nums: List[int]) -> List[List[int]]:
            ret = []
            def getPermutation(current_array, item_list):
                ret.append(current_array)
                if not item_list: return
                for idx in range(len(item_list)):
                    getPermutation(current_array+[item_list[idx]], (item_list[idx+1:]))
    
            getPermutation([], nums)
            return ret

     

    2. One-liner ํ’€์ด

    ํŒŒ์ด์ฌ์ด๋ผ๋Š” ์–ธ์–ด ํŠน์„ฑ ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ณต์žกํ•ด๋ณด์—ฌ๋„ ํ•œ์ค„์งœ๋ฆฌ ํ’€์ด(?) ๊ฐ€ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

    ์•„๋ž˜ ํ’€์ด๋Š” nums์˜ ์ด์ดํ…œ์„ ๋Œ๋ฉด์„œ ์ „์ฒด ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 

    ์˜ˆ๋ฅผ๋“ค์–ด ์ฃผ์–ด์ง„ nums : [1,2,3] ์ด์—ˆ๋‹ค๋ฉด

    for ๋ฌธ์„ ๋Œ ๋•Œ ๋งˆ๋‹ค ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๋€Œ๊ฒ ๋„ค์š”

    [[]]

    [[], [1]]

    [[], [1], [2], [1, 2]]

    [[], [1], [2], [1, 2], [3], [1,3], [2,3], [1,2,3]

    ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ์•„์ดํ…œ์„ ๋ณผ๋“œ๋กœ ํ‘œ์‹œํ–ˆ๋Š”๋ฐ, ๋ณด๋ฉด ์›๋ž˜ ์žˆ๋˜ ๋ฐฐ์—ด์— item๊ฐ’์„ ์ถ”๊ฐ€ํ•œ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.

     

    class Solution:
        def subsets(self, nums: List[int]) -> List[List[int]]:
            if not nums: return [[]]
            ret = [[]]
            for item in nums: ret += [current + [item] for current in ret]
            return ret
    ๋ฐ˜์‘ํ˜•