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]]
Constraints:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums
are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for finding all possible orderings of items involves trying every single possible arrangement. We generate each permutation and add the valid ones to our result. This guarantees we find all possible answers, although it might take a while if we have a lot of items.
Here's how the algorithm would work step-by-step:
def find_all_permutations(input_list):
all_possible_permutations = []
def generate_permutations(current_permutation, remaining_elements):
# If there are no remaining elements, the permutation is complete.
if not remaining_elements:
all_possible_permutations.append(current_permutation[:])
return
for index in range(len(remaining_elements)):
# Choose an element to add to the current permutation.
element_to_add = remaining_elements[index]
current_permutation.append(element_to_add)
# Create a new list of remaining elements without the chosen one.
new_remaining_elements = remaining_elements[:index] + remaining_elements[index+1:]
# Recursively generate permutations.
generate_permutations(current_permutation, new_remaining_elements)
# Backtrack by removing the last added element to explore other possibilities.
current_permutation.pop()
generate_permutations([], input_list)
return all_possible_permutations
To create all possible arrangements of items, we'll use a method that systematically explores each arrangement. The core idea is to build up each arrangement step-by-step by making choices about which item to include next, and then undo those choices to explore other possibilities. This avoids generating duplicate arrangements and ensures we consider every possibility exactly once.
Here's how the algorithm would work step-by-step:
def find_all_permutations(input_list):
all_possible_permutations = []
current_permutation = []
used_elements = [False] * len(input_list)
def generate_permutations():
# When permutation is complete, add to results
if len(current_permutation) == len(input_list):
all_possible_permutations.append(current_permutation[:] )
return
for index in range(len(input_list)):
# Only consider unused elements to avoid duplicates in permutation
if not used_elements[index]:
current_permutation.append(input_list[index])
used_elements[index] = True
generate_permutations()
# Backtrack: remove the last element to explore other branches
used_elements[index] = False
current_permutation.pop()
generate_permutations()
return all_possible_permutations
Case | How to Handle |
---|---|
Null or empty input array | Return an empty list to indicate no permutations are possible for an invalid input. |
Input array with only one element | Return a list containing a list with the single element as the only permutation. |
Input array with two elements | Ensure both possible permutations are generated correctly by swapping the elements. |
Large input array causing stack overflow in recursive implementations | Consider using iterative solutions or language-specific optimizations for tail recursion to avoid stack overflow issues. |
Input array containing negative numbers | The algorithm should function correctly with negative numbers without any special modifications. |
Input array containing zero | The algorithm should function correctly with zero without any special modifications. |
Input array with elements that are near the maximum integer value | Ensure that any arithmetic operations within the algorithm do not lead to integer overflows, which could cause incorrect results. |
Input array that is already sorted | The algorithm should still generate all permutations correctly, regardless of the initial order of elements. |