From 043507e55e19cd2b0c3f69a615176c7fe979075b Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Fri, 13 Jan 2023 19:52:50 -0500 Subject: [PATCH] Time: 47 ms (63.61%), Space: 14.1 MB (56.56%) - LeetHub --- 46-permutations/46-permutations.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 46-permutations/46-permutations.py diff --git a/46-permutations/46-permutations.py b/46-permutations/46-permutations.py new file mode 100644 index 0000000..f87fe6b --- /dev/null +++ b/46-permutations/46-permutations.py @@ -0,0 +1,15 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + def permute_with_prefix(nums: List[int], prefix: List[int]): + # Base case + if len(nums) == 1: + return [prefix + nums] + + permutations = [] + for i in range(len(nums)): + permutations += permute_with_prefix(nums[:i] + nums[(i + 1):], # Remove the ith element from nums + prefix + [nums[i]]) + return permutations + + # Initial call + return permute_with_prefix(nums, []) \ No newline at end of file