LeetCode Problem

How to Solve Kth Smallest Instructions

The Kth Smallest Instructions problem asks for the kth lexicographically smallest instruction sequence to travel in a grid from (0, 0) to a destination. By applying state transition dynamic programming, this problem can be efficiently solved by tracking the number of possible paths at each step and making decisions based on combinations, ensuring the correct instruction sequence is generated.

GhostInterview Help

Need help with Kth Smallest Instructions without spending extra time grinding it?

GhostInterview can read Kth Smallest Instructions from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1643State transition dynamic programmingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The Kth Smallest Instructions problem asks for the kth lexicographically smallest instruction sequence to travel in a grid from (0, 0) to a destination. By applying state transition dynamic programming, this problem can be efficiently solved by tracking the number of possible paths at each step and making decisions based on combinations, ensuring the correct instruction sequence is generated.

Problem Statement

Bob is at the top-left corner of a grid and wants to move to a destination at (row, column). He can only move right ('H') or down ('V'). There are multiple ways to reach the destination, and the goal is to determine the kth smallest lexicographical instruction sequence that leads Bob to the destination.

Each instruction sequence consists of exactly row 'H' moves and column 'V' moves. The number of such sequences is given by nCr(row + column, row). The challenge is to compute the kth sequence efficiently using a combination of dynamic programming and combinatorics, while maintaining the correct order of movements.

Examples

Example 1

Input: destination = [2,3], k = 1

Output: "HHHVV"

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 details omitted.

Example 3

Input: destination = [2,3], k = 3

Output: "HHVVH"

Example details omitted.

Constraints

  • destination.length == 2
  • 1 <= row, column <= 15
  • 1 <= k <= nCr(row + column, row), where nCr(a, b) denotes a choose b​​​​​.

Solution Approach

State Transition Dynamic Programming

The problem can be modeled as a state transition where at each step, you decide whether to move right ('H') or down ('V'). By using dynamic programming, we track the number of possible paths from the current state to the destination and make decisions based on lexicographical order. The DP table stores how many ways there are to reach each intermediate point, allowing us to generate the kth sequence without having to enumerate all paths.

Combinatorics for Path Count Calculation

At each step, we need to decide whether moving right or down yields the kth sequence. The number of possible paths from a given point can be determined using combinatorics. Specifically, the number of paths from (x, y) to (row, column) is calculated using the binomial coefficient, nCr(row - x + column - y, row - x). This allows us to efficiently determine which move corresponds to the kth sequence.

Efficient Calculation of the kth Sequence

Instead of generating all possible paths, we can directly compute the kth sequence by progressively deciding whether to move right or down. For each decision point, we use the precomputed DP table and binomial coefficients to choose the appropriate move. This avoids the need to generate and sort all paths, making the solution efficient even for larger grid sizes.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of this approach is dominated by the computation of binomial coefficients and dynamic programming state transitions. Since there are row + column steps, the time complexity is O(row * column), and each decision step involves calculating binomial coefficients in constant time. The space complexity is O(row * column) due to the DP table used to store intermediate results.

What Interviewers Usually Probe

  • The candidate demonstrates understanding of dynamic programming and combinatorics.
  • The candidate efficiently applies binomial coefficients to reduce the problem complexity.
  • The candidate navigates the state transition approach to solve the problem without brute-forcing all paths.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to consider the lexicographical order of instructions, leading to incorrect kth sequences.
  • Miscomputing the binomial coefficients, resulting in an inefficient or incorrect path count.
  • Not using dynamic programming to optimize the calculation of intermediate results, leading to unnecessary recomputation.

Follow-up variants

  • Given a different destination (row, column), how would the approach change?
  • How would you modify the approach for finding the largest lexicographical instruction sequence?
  • What happens if there are constraints on the number of right or down moves allowed?

How GhostInterview Helps

  • GhostInterview helps by providing an optimized approach that uses dynamic programming to solve the problem efficiently, even for larger inputs.
  • It assists in reducing the complexity of the problem by applying combinatorics to calculate possible paths without having to generate them all.
  • GhostInterview guides you to avoid unnecessary computations and stay focused on state transitions, ensuring an optimal and correct solution.

Topic Pages

FAQ

What is the main approach for solving the Kth Smallest Instructions problem?

The main approach involves using state transition dynamic programming to track the number of possible paths, combined with combinatorial calculations to directly compute the kth smallest lexicographic sequence.

How do you calculate the number of possible paths from a point to the destination?

The number of possible paths is calculated using binomial coefficients, specifically nCr(row - x + column - y, row - x), where (x, y) is the current point and (row, column) is the destination.

Why is dynamic programming important in solving this problem?

Dynamic programming helps by storing intermediate results, which avoids recomputing the number of paths multiple times, thus optimizing the solution and making it feasible for larger grids.

What are the constraints for the Kth Smallest Instructions problem?

The constraints are that destination.length == 2, 1 <= row, column <= 15, and 1 <= k <= nCr(row + column, row), where nCr is the binomial coefficient.

What happens if k exceeds the total number of possible paths?

If k exceeds the total number of paths from (0, 0) to (row, column), the problem would not have a valid kth sequence, and the answer should handle such edge cases.

GhostInterview Solver

Need direct help with Kth Smallest Instructions instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Kth Smallest Instructions from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.