forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
31 lines (31 loc) · 904 Bytes
/
Solution.java
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
class Solution {
public String kthSmallestPath(int[] destination, int k) {
int v = destination[0], h = destination[1];
int n = v + h;
int[][] c = new int[n + 1][h + 1];
c[0][0] = 1;
for (int i = 1; i <= n; ++i) {
c[i][0] = 1;
for (int j = 1; j <= h; ++j) {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
}
StringBuilder ans = new StringBuilder();
for (int i = n; i > 0; --i) {
if (h == 0) {
ans.append('V');
} else {
int x = c[v + h - 1][h - 1];
if (k > x) {
ans.append('V');
k -= x;
--v;
} else {
ans.append('H');
--h;
}
}
}
return ans.toString();
}
}