All problems
0237HardMathRecursion

Nozzle Firing Book

Tracked in this browser only
Write code

Trains the technique from

LeetCode 60Permutation Sequence

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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