Show problems as
Classic problem #60HardMathRecursion

Permutation Sequence

Real-world version: Nozzle Firing Book
Tracked in this browser only
Read Permutation Sequence on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration rig drives n nozzles, numbered 1 through n. Its test book holds every firing order of those nozzles exactly once, one order per line, each written as the nozzle numbers run together with nothing between them.

Every line of the book has the same number of digits, and the book is sorted by reading each line as a number and placing smaller numbers earlier. With n = 3 the book runs 123, then 132, then 213, then 231, then 312, and finally 321.

Given n and a line number k, with the opening line counted as line 1, return that line of the book as a string.

Examples

Example 1

Input
n = 4, k = 7
Output
"2134"

Line 7 of the four-nozzle book reads 2134, which fires each of the nozzles 1, 2, 3 and 4 once.

Example 2

Input
n = 5, k = 13
Output
"14235"

Line 13 of the five-nozzle book reads 14235, and every nozzle from 1 to 5 appears in it exactly once.

Example 3

Input
n = 1, k = 1
Output
"1"

One nozzle admits one firing order, so the book has the single line 1.

Constraints

  • 1 <= n <= 9
  • 1 <= k <= n!

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def firing_order(n: int, k: int) -> str:
Java
public String firingOrder(int n, int k)