-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsol.c
38 lines (34 loc) · 876 Bytes
/
sol.c
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
long long comb(int n, int k) {
long long res = 1;
for (int i = 1; i <= k; i++) {
res = res * (n - i + 1) / i;
}
return res;
}
char* kthSmallestPath(int* destination, int destinationSize, int k) {
int v = destination[0]; // număr de V
int h = destination[1]; // număr de H
int total = v + h;
char *path = malloc(total + 1);
path[total] = '\0';
for (int i = 0; i < total; i++) {
if (h == 0) {
path[i] = 'V';
v--;
} else if (v == 0) {
path[i] = 'H';
h--;
} else {
long long countH = comb(h + v - 1, h - 1);
if (k <= countH) {
path[i] = 'H';
h--;
} else {
path[i] = 'V';
v--;
k -= countH;
}
}
}
return path;
}