#60

Permutation Sequence

Hard Factorial number system

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

The permutations of 1..n are listed in sorted order. Return the k-th one as a string.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the k-th permutation of 1..n in sorted order, without listing them all.

Why this works: each choice of first digit covers (n-1)! permutations, so (k-1) / (n-1)! picks the first digit; repeat on the rest.

Example: n = 3, k = 3 (0-based 2)

2 / 2! = 1 → second of [1,2,3] → 2, k = 00 / 1! = 0 → 1, then 3"213"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using 1-based k directly, which is off by one at block boundaries.

Solve on LeetCode ↗
Complexity
  • Time: O(n²) because of list removals (n ≤ 9).
  • Space: O(n).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon