-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_214288.java
More file actions
73 lines (61 loc) Β· 2.64 KB
/
HW_214288.java
File metadata and controls
73 lines (61 loc) Β· 2.64 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
class HW_214288 {
public int solution(int k, int n, int[][] reqs) {
int answer = Integer.MAX_VALUE; // μ΅μ λκΈ° μκ°μ μ μ₯ν λ³μ
// κ°λ₯ν λ©ν λ°°μ μ‘°ν© μμ±
List<int[]> mentors = make(k, n);
// κ° λ°°μ μ‘°ν©μ λν΄ μ΅μ λκΈ° μκ°μ κ³μ°
for (int i = 0; i < mentors.size(); i++) {
int[] dist = mentors.get(i);
int wait = simulation(dist, reqs);
answer = Math.min(answer, wait);
}
return answer;
}
// λ©ν λ°°μ μ‘°ν© μμ±
private List<int[]> make(int k, int n) {
List<int[]> result = new ArrayList<>();
combination(new int[k], 0, n - k, result); // nλͺ
μ λ©ν λ₯Ό kκ°μ μ νμ μ΅μ ν λͺ
μ© λ°°μ
return result;
}
// μ¬κ·μ μΌλ‘ λ©ν λ°°μ μ‘°ν©μ μμ±
private void combination(int[] dist, int idx, int remaining, List<int[]> result) {
if (idx == dist.length) {
if (remaining == 0) {
int[] newDist = new int[dist.length];
for (int i = 0; i < dist.length; i++) {
newDist[i] = dist[i];
}
result.add(newDist);
}
return;
}
for (int i = 1; i <= remaining + 1; i++) {
dist[idx] = i;
combination(dist, idx + 1, remaining - (i - 1), result);
}
}
// μ£Όμ΄μ§ λ©ν λ°°μ μ‘°ν©μ λν΄ κ° μλ΄ μ νλ³ λκΈ° μκ° ν©μ°
private int simulation(int[] dist, int[][] reqs) {
Map<Integer, PriorityQueue<Integer>> mentorMap = new HashMap<>();
for (int i = 0; i < dist.length; i++) {
mentorMap.put(i + 1, new PriorityQueue<>(dist[i]));
for (int j = 0; j < dist[i]; j++) {
mentorMap.get(i + 1).offer(0); // μ΄κΈ° μλ΄ κ°λ₯ μκ°μ 0μΌλ‘ μ€μ
}
}
int total = 0; // μ΄ λκΈ° μκ°
// μλ΄ μμ² μμλλ‘ μ²λ¦¬
for (int i = 0; i < reqs.length; i++) {
int start = reqs[i][0]; // μλ΄ μμ² μκ°
int ing = reqs[i][1]; // μλ΄ μκ°
int type = reqs[i][2]; // μλ΄ μ ν
PriorityQueue<Integer> mentors = mentorMap.get(type);
int next = mentors.poll(); // ν΄λΉ μ νμ λ©ν λ€ μ€ κ°μ₯ 빨리 μλ΄ κ°λ₯ν λ©ν μ ν
int wait = Math.max(0, next - start); // λκΈ° μκ° κ³μ°
total += wait;
mentors.offer(Math.max(start, next) + ing); // λ©ν μ λ€μ μλ΄ κ°λ₯ μκ° μ
λ°μ΄νΈ
}
return total;
}
}