Kth Smallest Instructions LeetCode solution in Python
1from math import comb234class Solution:5def kthSmallestPath(self, destination: List[int], k: int) -> str:6i, j = destination78@lru_cache(None)9def helper(i, j, k):10if k == 1:11return "H" * j + "V" * i12else:13horizontal = comb(i + j - 1, j - 1)14if k <= horizontal:15return "H" + helper(i, j - 1, k)16else:17return "V" + helper(i - 1, j, k - horizontal)1819return helper(i, j, k)