Java Beats 100.00%
\n\n# Code\njava []\nclass Solution {\n    public String kthSmallestPath(int[] destination, int k) {\n        int v = destination[0];  // number of vertical mov
\n\n# Code\n```java []\nclass Solution {\n    public String kthSmallestPath(int[] destination, int k) {\n        int v = destination[0];  // number of vertical moves needed\n        int h = destination[1];  // number of horizontal moves needed\n        int totalLen = v + h;    // total length of the path\n        \n        // StringBuilder to construct the result\n        StringBuilder result = new StringBuilder();\n        \n        // For each position, decide whether to put \'H\' or \'V\'\n        for (int i = 0; i < totalLen; i++) {\n            // Calculate remaining positions to fill\n            int remainingLen = totalLen - i - 1;\n            \n            if (h > 0) {  // if we can still go horizontal\n                // Calculate number of combinations if we put \'H\' here\n                // This represents number of paths lexicographically smaller than putting \'V\'\n                long combinations = combination(remainingLen, v);\n                \n                if (combinations >= k) {\n                    // If combinations >= k, we should put \'H\' here\n                    result.append(\'H\');\n                    h--;\n                } else {\n                    // If combinations < k, we should put \'V\' here\n                    result.append(\'V\');\n                    v--;\n                    k -= combinations;\n                }\n            } else {\n                // If no more horizontal moves left, must go vertical\n                result.append(\'V\');\n                v--;\n            }\n        }\n        \n        return result.toString();\n    }\n    \n    // Helper method to calculate combinations (n choose r)\n    private long combination(int n, int r) {\n        if (r > n) return 0;\n        if (r == 0 || r == n) return 1;\n        \n        r = Math.min(r, n - r); // optimize by using the smaller value\n        long result = 1;\n        \n        for (int i = 1; i <= r; i++) {\n            result *= (n - i + 1);\n            result /= i;\n        }\n        \n        return result;\n    }\n}\n```
['Java']