-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (29 loc) · 756 Bytes
/
Solution.java
File metadata and controls
32 lines (29 loc) · 756 Bytes
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
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
int n = 3;
int k = 5;
System.out.println(Arrays.toString(solution(n, k)));
}
public static int[] solution(int n, long k) {
List<Integer> list = new ArrayList<>();
long fac = 1;
for(int i=1;i<=n;i++) {
fac*=i;
list.add(i);
}
k--;
int idx = 0;
int[] result = new int[n];
while(n>0) {
fac /= n--;
result[idx] = list.get((int) (k/fac));
list.remove((int) (k/fac));
k %= fac;
idx++;
}
return result;
}
}