Advertisement

Permutation Sequence - LeetCode 60 Solution

Permutation Sequence - Complete Solution Guide

Permutation Sequence is LeetCode problem 60, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: n = 3, k = 3 Output: "213" Example 2: Input: n = 4, k = 9 Output: "2314" Example 3: Input: n = 3, k = 1 Output: "123" Constraints: 1 <= n <= 9 1 <= k <= n!

Detailed Explanation

The problem asks us to find the k-th permutation in a sequence of all possible permutations of the numbers 1 to n. The permutations are listed in lexicographical order. Given n and k, we need to return the string representation of the k-th permutation. For example, if n = 3, the permutations are "123", "132", "213", "231", "312", "321". If k = 3, the answer is "213". The constraints are 1 <= n <= 9 and 1 <= k <= n!.

Solution Approach

The solution uses a mathematical approach to find the k-th permutation without generating all permutations. It iteratively determines each digit of the permutation by dividing k by factorials of decreasing numbers. It maintains a list of available numbers and removes the chosen number at each step. The algorithm cleverly uses the factorials to compute the correct index of each element in the kth permutation.

Step-by-Step Algorithm

  1. Step 1: Create a list of numbers from 1 to n.
  2. Step 2: Decrement k by 1 because the sequence is 0-indexed in our calculation.
  3. Step 3: Iterate from n-1 down to 0.
  4. Step 4: Calculate the factorial of the current iterator i (i.e., (n-1)!, (n-2)!, ..., 0!).
  5. Step 5: Calculate the index of the digit to select by dividing k by the factorial calculated in step 4 (index = k // factorial).
  6. Step 6: Append the digit at the calculated index to the permutation string, and remove it from the list of available numbers.
  7. Step 7: Update k to be the remainder of the division in step 5 (k = k % factorial).
  8. Step 8: Repeat steps 3-7 until all digits are selected.
  9. Step 9: Return the generated permutation string.

Key Insights

  • Insight 1: We don't need to generate all n! permutations. That would be very inefficient, especially for larger values of n.
  • Insight 2: We can determine the digits of the k-th permutation one by one. By dividing k by the factorial of (n-1), we can figure out which number should be the first digit.
  • Insight 3: After choosing the first digit, we update k to the remainder of the division and continue with the remaining digits.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Math, Recursion.

Companies

Asked at: Adobe, Amazon, Jump Trading, X.