46. Permutations

๐Ÿ•ต๏ธ‍โ™€๏ธ Question


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

 

 

๐Ÿ’ก Example


1:

Input: nums = [1,2,3]

Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

 

2:

Input: nums = [0,1]

Output: [[0,1],[1,0]]

 

3:

Input: nums = [1]

Output: [[1]]

 

 

 

๐Ÿ“Œ Constraints


  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • All the integers of nums are unique.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


์„œ๋กœ ๋‹ค๋ฅธ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ˆ˜์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ˆœ์—ด ๋ฌธ์ œ๋‹ค.

๋‘๊ฐ€์ง€์˜ ํ’€์ด๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

version1 : DFS๋ฅผ ํ™œ์šฉํ•œ ์ˆœ์—ด ์ƒ์„ฑ

๋ฐฑํŠธ๋ž˜ํ‚น(backtracking)์ด๋ž€? :

ํ•ด๋ฅผ ์ฐพ๋Š” ๋„์ค‘ ํ•ด๊ฐ€ ์•„๋‹ˆ์–ด์„œ ๋ง‰ํžˆ๋ฉด, ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ ํ™” ๋ฌธ์ œ์™€ ๊ฒฐ์ • ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ๋ฉ๋‹ˆ๋‹ค.

์ˆœ์—ด์ด๋ž€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ทธ๋ž˜ํ”„ ํ˜•ํƒœ๋กœ ๋‚˜์—ดํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. -> ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

The graph of Permutation with backtracking

# Runtime: 69 ms
# Memory Usage: 14.4 MB

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        results = []
        prev_elements = []
        
        def dfs(elements) :
            #๋ฆฌํ”„๋…ธ๋“œ์ผ๋•Œ
            if len(elements) == 0 :
                results.append(prev_elements[:])
                
            #์ˆœ์—ด ์ƒ์„ฑ ์žฌ๊ท€ ํ˜ธ์ถœ
            for e in elements:
                next_elements = elements[:]
                next_elements.remove(e)
               
                prev_elements.append(e)
                # print("perv",prev_elements)
                # print("next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„",next_elements)
                dfs(next_elements)
                prev_elements.pop()
                # print("**๋ฐฑํŠธ๋ž˜ํ‚น",prev_elements)
    
        dfs(nums) 
        return results
# input์ด [1,2,3] ๋ฉด ๋‚˜์˜ค๋Š” ์ถœ๋ ฅ,,,
perv [1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2, 3]
perv [1, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [3]
perv [1, 2, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1, 2]
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
perv [1, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2]
perv [1, 3, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1, 3]
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1, 3]
perv [2, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [3]
perv [2, 1, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [2, 1]
**๋ฐฑํŠธ๋ž˜ํ‚น [2]
perv [2, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [2, 3, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [2, 3]
**๋ฐฑํŠธ๋ž˜ํ‚น [2]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1, 2]
perv [3, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2]
perv [3, 1, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [3, 1]
**๋ฐฑํŠธ๋ž˜ํ‚น [3]
perv [3, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [3, 2, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [3, 2]
**๋ฐฑํŠธ๋ž˜ํ‚น [3]
**๋ฐฑํŠธ๋ž˜ํ‚น []
=========
perv [0]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [0, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [0]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [0]
perv [1, 0]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
**๋ฐฑํŠธ๋ž˜ํ‚น []

 

 

 

version2. ํŒŒ์ด์ฌ์˜ itertools ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค.

itertools 
๋ฐ˜๋ณต์ž ์ƒ์„ฑ์— ์ตœ์ ํ™”๋œ ํšจ์œจ์ ์ธ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณต.
์ด๋ฏธ ์ž˜ ๊ตฌํ˜„๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ ๋ฒ„๊ทธ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ๊ณ  ํšจ์œจ์ ์œผ๋กœ ์„ค๊ณ„๋œ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ ์†๋„๋„ ๋น ๋ฆ„
๐Ÿ“ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์„์œผ๋กœ "# ๊ตฌํ˜„์˜ ํšจ์œจ์„ฑ, ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค" ๋ผ๊ณ  ๋‹ฌ์•„์ฃผ๋ฉด ์ต์˜ค~

permutation( ) ํ•จ์ˆ˜๊ฐ€ ํŠœํ”Œ ๋ชจ์Œ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌํŠธ์ฝ”๋“œ ๋ฌธ์ œ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€ํ™˜์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

# Runtime: 28 ms
# Memory Usage: 14.3 MB

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        import itertools
        return list(map(list, itertools.permutations(nums)))

+ Recent posts