File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ package week02 .PGS_줄서는방법 ;
2+
3+ import java .util .*;
4+
5+ class Solution {
6+ static long [] factorial ;
7+
8+ public int [] solution (int n , long k ) {
9+ //1 ~ n 숫자 리스트
10+ ArrayList <Integer > list = new ArrayList <>();
11+ for (int i = 1 ; i <= n ; i ++) {
12+ list .add (i );
13+ }
14+
15+ //팩토리얼 결과 배열
16+ factorial = new long [n + 1 ];
17+ getFactorialArr (n );
18+
19+ //정답 배열 구하기
20+ int num = n ;
21+ int [] answer = new int [n ];
22+ int idx = 0 ;
23+
24+ while (n > 1 ) {
25+ long blockCnt = factorial [n -1 ]; //한 블록의 개수
26+ int q = (int ) (k / blockCnt );
27+ int order = k % blockCnt == 0 ? q : q + 1 ; //몇 번째 블록인지
28+
29+ answer [idx ++] = list .remove ((int ) (order - 1 )); //order번째 수 제거 + 답 저장
30+ k -= (order - 1 ) * blockCnt ; //이전까지의 블록 개수만큼 제거
31+ n --;
32+ }
33+
34+ answer [num -1 ] = list .get (0 );
35+ return answer ;
36+ }
37+
38+ void getFactorialArr (int n ) {
39+ factorial [1 ] = 1 ;
40+ for (int i = 2 ; i <= n ; i ++) {
41+ factorial [i ] = factorial [i -1 ] * i ;
42+ }
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments