1
+ import java .util .*;
2
+
3
+ class Solution {
4
+
5
+ static int N , K ;
6
+ static List <int []> kList ;
7
+
8
+ public int solution (int k , int n , int [][] reqs ) {
9
+ int answer = Integer .MAX_VALUE ;
10
+ N = n ;
11
+ K = k ;
12
+
13
+ kList = new ArrayList <>();
14
+ // 상담원 분배: 중복 순열
15
+ int [] krr = new int [K +1 ];
16
+ makeList (1 , 0 , krr );
17
+
18
+ // 상담 진행
19
+ for (int [] c : kList ) {
20
+ answer = Math .min (answer , consult (c , reqs ));
21
+ }
22
+
23
+ return answer ;
24
+ }
25
+ public static void makeList (int depth , int total , int [] krr ) {
26
+ if (depth == K +1 ) {
27
+ if (total == N ) {
28
+ // System.out.println(Arrays.toString(krr));
29
+ // 배열 복사
30
+ int [] nKrr = Arrays .copyOf (krr , krr .length );
31
+ kList .add (nKrr );
32
+ }
33
+ return ;
34
+ }
35
+
36
+ for (int i =1 ; i <N +1 ; i ++) {
37
+ krr [depth ] = i ;
38
+ makeList (depth +1 , total +i , krr );
39
+ }
40
+ }
41
+ public static int consult (int [] krr , int [][] reqs ) {
42
+ PriorityQueue <Integer >[] mento = new PriorityQueue [K +1 ];
43
+
44
+ // 유형별로 우선순위 큐를 사용해 가장 상담이 빨리 끝나는 상담원을 출력
45
+ // 각 타입에 분배한 멘토인원(krr)만큼
46
+ // 각 멘토의 끝나는 시간 추가, 초기값은 모두 0
47
+ for (int i =1 ; i <K +1 ; i ++) {
48
+ mento [i ] = new PriorityQueue <>();
49
+ for (int j =0 ; j <krr [i ]; j ++){
50
+ mento [i ].add (0 );
51
+ }
52
+
53
+ }
54
+
55
+ int total = 0 ;
56
+ for (int [] req : reqs ) {
57
+ int type = req [2 ];
58
+ int endTime = mento [type ].poll ();
59
+ // 기다린 시간 (기다리자 않았다면 0)
60
+ int waitTime = Math .max (0 , endTime - req [0 ]);
61
+ total += waitTime ;
62
+ // 현재 상담자 req의 상담이 끝나는 시간 기록
63
+ mento [type ].add (waitTime + req [0 ] + req [1 ]);
64
+ }
65
+
66
+ return total ;
67
+ }
68
+ }
0 commit comments