-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathkth-smallest-instructions.cpp
38 lines (36 loc) · 1.09 KB
/
kth-smallest-instructions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Time: O((m + n)^2)
// Space: O(1)
class Solution {
public:
string kthSmallestPath(vector<int>& destination, int k) {
int r = destination[0], c = destination[1];
string result;
while (r + c) {
int count = nCr(r + (c - 1), r); // the number of HX..X combinations
if (k <= count) { // the kth instruction is one of HX..X combinations, so go right
--c;
result.push_back('H');
} else { // the kth instruction is one of VX..X combinations, so go down
k -= count; // the kth one of XX..X combinations is the (k-count)th one of VX..X combinations
--r;
result.push_back('V');
}
}
return result;
}
private:
uint64_t nCr(int n, int r) { // Time: O(n), Space: O(1)
if (n < r) {
return 0;
}
if (n - r < r) {
return nCr(n, n - r);
}
uint64_t c = 1;
for (int k = 1; k <= r; ++k) {
c *= n - k + 1;
c /= k;
}
return c;
}
};