Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f29516

Browse files
authoredNov 7, 2020
Update 1643-kth-smallest-instructions.js
1 parent e2cb751 commit 6f29516

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 

‎1643-kth-smallest-instructions.js

+38
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,41 @@ function comb(n, r) {
4949
return product(r + 1, n) / product(1, n - r)
5050
}
5151
}
52+
53+
// another
54+
55+
/**
56+
* @param {number[]} destination
57+
* @param {number} k
58+
* @return {string}
59+
*/
60+
const kthSmallestPath = function (destination, k) {
61+
const [r, c] = destination;
62+
const ret = [];
63+
let remDown = r;
64+
for (let i = 0; i < r + c; i++) {
65+
const remSteps = r + c - (i + 1);
66+
const com = comb(remSteps, remDown);
67+
if (com >= k) ret.push("H");
68+
else {
69+
remDown -= 1;
70+
k -= com;
71+
ret.push("V");
72+
}
73+
}
74+
return ret.join("");
75+
};
76+
77+
function comb(n, r) {
78+
if (n < r) return 0;
79+
let res = 1;
80+
if (n - r < r) r = n - r;
81+
for (let i = n, j = 1; i >= 1 && j <= r; --i, ++j) {
82+
res = res * i;
83+
}
84+
for (let i = r; i >= 2; --i) {
85+
res = res / i;
86+
}
87+
return res;
88+
}
89+

0 commit comments

Comments
 (0)
Please sign in to comment.