Kth Smallest Instructions - Problem

Imagine Bob is on a treasure hunt on a grid! Bob starts at the top-left corner (0, 0) and wants to reach his treasure at position (row, column). However, Bob can only move in two directions:

  • 'H' - Move horizontally (right)
  • 'V' - Move vertically (down)

For example, to reach (2, 3), Bob needs exactly 3 H's and 2 V's. Valid instruction sequences include "HHHVV", "HVHVH", "VHHVH", and many more!

Here's the twist: Bob has a lucky number k, and he wants the k-th lexicographically smallest instruction sequence. Since 'H' comes before 'V' alphabetically, "HHHVV" would come before "HVHVH".

Goal: Given the destination coordinates and Bob's lucky number k, return the k-th lexicographically smallest instruction sequence that will get Bob to his treasure!

Input & Output

example_1.py โ€” Basic Case
$ Input: destination = [2, 3], k = 1
โ€บ Output: "HHHVV"
๐Ÿ’ก Note: To reach (2,3), we need 3 H's and 2 V's. The lexicographically smallest sequence puts all H's first, giving us "HHHVV".
example_2.py โ€” Middle Sequence
$ Input: destination = [2, 3], k = 2
โ€บ Output: "HHVHV"
๐Ÿ’ก Note: The second lexicographically smallest sequence. After "HHHVV", we get "HHVHV" by moving one V earlier while keeping the lexicographic order.
example_3.py โ€” Last Sequence
$ Input: destination = [2, 3], k = 3
โ€บ Output: "HHVVH"
๐Ÿ’ก Note: The third lexicographically smallest sequence follows the pattern of incrementally moving V's to earlier positions.

Constraints

  • 1 โ‰ค destination[0], destination[1] โ‰ค 15
  • 1 โ‰ค k โ‰ค C(destination[0] + destination[1], destination[0])
  • k is guaranteed to be valid (within the range of possible sequences)

Visualization

Tap to expand
Smart Path Navigation: Combinatorics ApproachSTART(0,0)TREASURE(2,3)k=1: HHHVV (Optimal Route)Decision Tree AnalysisPos 1H:3, V:2, k=1Choose HChoose VC(3,2)=3kโ‰ค3 โœ“6 total seqsk>3 โœ—H:2,V:2H:1,V:2H:0,V:2Continue with V's...Final Result: "HHHVV"๐Ÿงฎ Mathematical ShortcutInstead of exploring all 10 possible paths,we use C(n,k) to calculate directly!โšก Time: O(m+n) vs Brute Force: O(2^(m+n))
Understanding the Visualization
1
Setup Navigation
Calculate total moves needed: 3 right (H) and 2 down (V) for destination (2,3)
2
Decision Point Analysis
At each intersection, calculate how many routes start by going right vs down
3
Smart Routing Choice
Use combinatorics to determine optimal direction without exploring all paths
4
Update Route Parameters
Adjust remaining moves and target route number based on chosen direction
Key Takeaway
๐ŸŽฏ Key Insight: By using combinatorics to count possibilities at each decision point, we can jump directly to the k-th solution without generating all permutations - like having a mathematical GPS that calculates the optimal route instantly!
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
21.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen