The set [1, 2, 3, ..., n]
contains a total of n!
unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3
:
"123"
"132"
"213"
"231"
"312"
"321"
Given n
and k
, return the k
-th permutation sequence.
For example:
n = 3
, k = 3
should return "213"
n = 4
, k = 9
should return "2314"
n = 3
, k = 1
should return "123"
Constraints:
1 <= n <= 9
1 <= k <= n!
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 goal is to find the kth permutation of a set of numbers. The brute force strategy involves generating all possible orderings (permutations) of the numbers and then simply picking the kth one from the complete list.
Here's how the algorithm would work step-by-step:
def permutation_sequence_brute_force(number_set_size, kth_permutation):
numbers = list(range(1, number_set_size + 1))
all_permutations = []
def generate_permutations(current_permutation, remaining_numbers):
if not remaining_numbers:
all_permutations.append(current_permutation[:])
return
for index in range(len(remaining_numbers)):
number = remaining_numbers[index]
current_permutation.append(number)
generate_permutations(current_permutation, remaining_numbers[:index] + remaining_numbers[index+1:])
current_permutation.pop()
generate_permutations([], numbers)
# Sort to ensure permutations are in lexicographical order
all_permutations.sort()
# Adjust for zero-based indexing to get correct permutation
return ''.join(map(str, all_permutations[kth_permutation - 1]))
The goal is to find the correct permutation without generating all permutations. The key idea is to understand how factorials relate to the position of a number within a permutation sequence, and then use that to pick each number in the correct order.
Here's how the algorithm would work step-by-step:
def get_permutation(number, position):
numbers = list(range(1, number + 1))
permutation = ''
position -= 1
factorial = 1
for i in range(1, number):
factorial *= i
for i in range(number - 1, -1, -1):
# Determine the index of the number to select
index = position // factorial
permutation += str(numbers[index])
numbers.pop(index)
# Update the position for the remaining numbers
position %= factorial
if i > 0:
factorial //= i
return permutation
Case | How to Handle |
---|---|
n is 0 or negative | Return an empty string or throw an exception, as the problem is not defined for non-positive n. |
k is 0 or negative | Throw an exception, as k must be a positive integer representing the kth permutation. |
k is greater than n! (factorial of n) | Throw an exception or return an empty string, as k exceeds the total number of possible permutations. |
n is 1 | Return "1" since there is only one permutation. |
n is large (e.g., n > 12) | Consider the potential for integer overflow when calculating factorials and use appropriate data types to mitigate it. |
k is 1 (first permutation) | Return the sorted sequence of numbers from 1 to n in ascending order. |
k is n! (last permutation) | Return the sequence of numbers from 1 to n in descending order. |
Integer overflow calculating factorials | Use a data type with a larger range, such as long, or implement a factorial function that detects and handles overflow. |