Kth Smallest Instructions Problem
Description
LeetCode Problem 1643.
Bob is standing at cell (0, 0), and he wants to reach destination: (row, column). He can only travel right and down. You are going to help Bob by providing instructions for him to reach destination.
The instructions are represented as a string, where each character is either:
'H', meaning move horizontally (go right), or'V', meaning move vertically (go down).
Multiple instructions will lead Bob to destination. For example, if destination is (2, 3), both "HHHVV" and "HVHVH" are valid instructions.
However, Bob is very picky. Bob has a lucky number k, and he wants the k^th lexicographically smallest instructions that will lead him to destination. k is 1-indexed.
Given an integer array destination and an integer k, return the k^th lexicographically smallest instructions that will take Bob to destination.
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"
Constraints:
destination.length == 21 <= row, column <= 151 <= k <= nCr(row + column, row), wherenCr(a, b)denotesachooseb.
Solution
Approach: Build the Answer Letter by Letter with Binomial Coefficients
Every instruction string has v = row letters 'V' and h = column letters 'H', in some order, and 'H' < 'V'. Listing all C(h + v, h) strings (up to about 155 million) is too slow, but we can decide one letter at a time.
At the current position, the strings that put 'H' here come before all the strings that put 'V' here. If we place 'H', the rest is any arrangement of h - 1 H's and v V's, so there are C(h + v - 1, h - 1) of them. Call this withH.
- If
k <= withH, the answer is among them: write'H'and use one H. - Otherwise it is not: skip all of them by subtracting
withHfromk, write'V', and use one V.
When no H is left, withH is 0 and the remaining letters are all 'V'. For destination = [2,3] and k = 3: C(4,2) = 6 strings start with H, so write H; then C(3,1) = 3 continue with H, still >= 3, so write H again; then only C(2,0) = 1 continues with H, so write V and k becomes 2; the same happens once more (k becomes 1), and the last letter is H. The answer is "HHVVH".
Precompute the binomial coefficients with Pascal's triangle up to 30; the largest, C(30, 15) = 155117520, fits in a 32-bit integer.
Complexity: O(h + v) time to build the string (plus a fixed 31 × 31 table). O(1) extra space apart from the output.
class Solution:
def kthSmallestPath(self, destination: List[int], k: int) -> str:
v, h = destination # moves down ('V') and right ('H') still to make
# comb[a][b] = a choose b, for a up to 30
comb = [[0] * 31 for _ in range(31)]
for a in range(31):
comb[a][0] = 1
for b in range(1, a + 1):
comb[a][b] = comb[a - 1][b - 1] + comb[a - 1][b]
ans = []
while h + v > 0:
# Paths that put 'H' here come first: there are C(h+v-1, h-1) of them.
with_h = comb[h + v - 1][h - 1] if h > 0 else 0
if k <= with_h:
ans.append("H")
h -= 1
else:
ans.append("V")
k -= with_h # skip all the paths that start with 'H'
v -= 1
return "".join(ans)