Difficulty: 🟡 Medium
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]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
1 <= nums.length <= 6
10 <= nums[i] <= 10
- All the integers of
nums
are unique.
Python3
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
size = len(nums)
if size == 1:
return [nums]
permutations = []
for i in range(size):
num = nums[i]
remaining_nums = nums[:i] + nums[i+1:]
for permutation in self.permute(remaining_nums):
permutations.append([num]+permutation)
return permutations
The provided solution uses a recursive approach to generate all possible permutations of the input array nums
. The key idea is to select one element from the array as the first element of each permutation and recursively generate permutations for the remaining elements. The base case occurs when the array has only one element, in which case the array itself is a valid permutation.
Here's a step-by-step explanation of the solution:
-
Initialize a variable
size
to store the length of the input arraynums
. -
Check if the value of
size
is equal to 1. Ifsize
is 1, it means there is only one element in the array, so return a list containing the array itself as the only permutation. This serves as the base case for the recursion. -
If
size
is greater than 1, create an empty listpermutations
to store the permutations. -
Iterate through the elements of the
nums
array using a loop variablei
from 0 tosize-1
. For each element at indexi
, do the following:- Extract the element
num
at indexi
. - Create a new list
remaining_nums
that contains all elements ofnums
except the one at indexi
. This is done using list slicing. - Recursively call the
permute
function with theremaining_nums
array to generate permutations for the remaining elements. The result is a list of lists representing permutations of the remaining elements.
- Extract the element
-
For each generated permutation of the remaining elements, append the
num
element (selected in step 4) as the first element of the permutation. This creates a new permutation withnum
as the first element. -
Repeat step 5 for all elements in the
nums
array, generating permutations for each possible starting element. -
After the loop completes, the
permutations
list will contain all possible permutations of thenums
array. -
Return the
permutations
list as the final result.
-
Time Complexity: The solution generates all possible permutations of the input array
nums
. The number of permutations is equal ton!
, wheren
is the length of the array. Therefore, the time complexity is O(n!), wheren
is at most 6 (as given in the problem constraints). As the input size is small (n <= 6), the time complexity is manageable. -
Space Complexity: The solution uses a constant amount of extra space for variables, and the space required for the
permutations
list is proportional to the number of permutations generated. Since there aren!
permutations, the space complexity is O(n!) in the worst case.
The provided solution effectively generates all possible permutations of the input array nums
using a recursive approach. It recursively selects one element as the first element of each permutation and generates permutations for the remaining elements. The base case is when there is only one element, in which case the array itself is returned as a permutation. The solution handles small input sizes efficiently.
NB: If you want to get community points please suggest solutions in other languages as merge requests.