#1643

Kth Smallest Instructions

candidate master · 1570 · lc hard +32 · verified · 44.7% accepted · 565 likes · top 27%

Description

Bob starts at (0, 0) and must reach destination = [row, column] moving only right ('H') or down ('V'). Many valid instruction strings exist. Return the kth lexicographically smallest instruction string (1-indexed).

Example 1:

Input: destination = [2,3], k = 1
Output: "HHHVV"
Explanation: All the instructions that reach (2, 3) in lexicographic order are as follows:
["HHHVV", "HHVHV", "HHVVH", "HVHHV", "HVHVH", "HVVHH", "VHHHV", "VHHVH", "VHVHH", "VVHHH"].

Example 2:

Input: destination = [2,3], k = 2
Output: "HHVHV"

Example 3:

Input: destination = [2,3], k = 3
Output: "HHVVH"

Code

1
2
3