1643. Kth Smallest Instructions

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1643

class Solution:
    def kthSmallestPath(self, destination: List[int], k: int) -> str:
        v, h = destination
        res = []
        for _ in range(h + v):
            if h == 0:
                res.append("V")
            else:
                x = comb(h + v - 1, h - 1)
                if k > x:
                    res.append("V")
                    v -= 1
                    k -= x
                else:
                    res.append("H")
                    h -= 1
        return "".join(res)