Tap here to switch tabs
Problem
Submissions

Problem: Kth Smallest Instructions

med
30 min
Try to solve the Kth Smallest Instructions problem.

Statement

Starting at cell (0,0)(0, 0), you want to reach the destination cell at coordinates (‘row‘,‘column‘)(`row`, `column`) given by destination =[‘row‘,‘column‘]= [`row`, `column`].

From any cell (r,c)(r, c), you can make one of the following moves:

  • Move down to (r+1,c)(r + 1, c), represented by the character 'V'.

  • Move right to (r,c+1)(r, c + 1), represented by the character 'H'.

A valid sequence of instructions consists of exactly row vertical moves 'V' and column horizontal moves 'H'.

There may be multiple valid instruction sequences that reach destination. When ordered lexicographically, 'H' comes before 'V'.

Given an array destination and an integer k, return the kextthk^{ ext{th}} lexicographically smallest instruction string to reach destination.

Constraints:

  • destination.length =2= 2

  • destination =[‘row‘,‘column‘]= [`row`, `column`]

  • 1≤1 \leq row, column ≤15\leq 15

  • 1≤1 \leq k ≤extnCr(‘row‘+‘column‘,‘row‘)\leq ext{nCr}(`row` + `column`, `row`), where $ ext{nCr}(a, b)$ denotes aa choose bb

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Kth Smallest Instructions

med
30 min
Try to solve the Kth Smallest Instructions problem.

Statement

Starting at cell (0,0)(0, 0), you want to reach the destination cell at coordinates (‘row‘,‘column‘)(`row`, `column`) given by destination =[‘row‘,‘column‘]= [`row`, `column`].

From any cell (r,c)(r, c), you can make one of the following moves:

  • Move down to (r+1,c)(r + 1, c), represented by the character 'V'.

  • Move right to (r,c+1)(r, c + 1), represented by the character 'H'.

A valid sequence of instructions consists of exactly row vertical moves 'V' and column horizontal moves 'H'.

There may be multiple valid instruction sequences that reach destination. When ordered lexicographically, 'H' comes before 'V'.

Given an array destination and an integer k, return the kextthk^{ ext{th}} lexicographically smallest instruction string to reach destination.

Constraints:

  • destination.length =2= 2

  • destination =[‘row‘,‘column‘]= [`row`, `column`]

  • 1≤1 \leq row, column ≤15\leq 15

  • 1≤1 \leq k ≤extnCr(‘row‘+‘column‘,‘row‘)\leq ext{nCr}(`row` + `column`, `row`), where $ ext{nCr}(a, b)$ denotes aa choose bb