updated at 2023. 2. 1. 오후 11:55:46

This commit is contained in:
rl544
2023-02-01 23:55:46 +09:00
parent 16cf353b65
commit f567c192bf
+6 -12
View File
@@ -1,21 +1,15 @@
// [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/12936 // [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/12936
import java.util.*; import java.util.ArrayList;
class Solution { class Solution {
public int[] solution(int n, long k) { public int[] solution(int n, long k) {
ArrayList<Integer> hs = new ArrayList<>(); ArrayList<Integer> al = new ArrayList<>();
int[] test = new int[n]; int[] answer = new int[n];
int pos, len = n; int pos, len = n;
long div, tmp, fact = 1; long div, tmp, fact = 1;
for(int f = n; f >= 2; f--) fact *= f; for(int f = n; f >= 2; f--) fact *= f;
for(int a = 1; a <= len; a ++) hs.add(a); for(int a = 1; a <= len; a ++) al.add(a);
for(int a = 0; a < len; a++, tmp = pos*div, k -= tmp, fact /= n, n--){ for(int a = 0; a < len; div = fact/n, pos = (int)((k-1)/div), answer[a] = al.remove(pos), k -= pos*div, fact /= n, n--, a++) continue; // 별 의미는 없지만 이런 식으로 안의 내용을 for 괄호 내에 다 때려박아서 줄일수 있다.
div = (fact/n); return answer;
pos = (int)((k-1)/div);
test[a] = hs.get(pos);
hs.remove(pos);
}
return test;
} }
} }