Repository navigation
Expand file tree
/
Copy pathDH_389479.java
More file actions
50 lines (39 loc) Β· 1.53 KB
/
Copy pathDH_389479.java
File metadata and controls
50 lines (39 loc) Β· 1.53 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
import java.util.*;
/*
μλ² μ¦μ€ νμ
*/
class DH_389479 {
static class Node implements Comparable<Node> {
int s, e;
public Node(int s, int e) {
this.s = s;
this.e = e;
}
@Override
public int compareTo(Node o) {
if(this.s != o.s) return Integer.compare(this.s, o.s); // μμ μμ
return Integer.compare(this.e, o.e); // λλλ μμ
}
}
public int solution(int[] players, int m, int k) {
Queue<Node> q = new ArrayDeque<>();
int answer = 0;
for(int i = 0; i < players.length; i++) {
while(!q.isEmpty() && q.peek().e <= i) q.poll();
int serverCnt = q.size(); // νμ¬ μ¬μ©λλ μλ²μ μ
// νμ¬ μλ²λ‘ μ¬μ©μλ€μ΄ κ²μμ ν μ μμ λ
if(players[i] < (serverCnt + 1) * m) continue;
// μΆκ°ν΄μΌλλ μλ² κ°μ ꡬνκΈ°
int sub = players[i] - (serverCnt + 1) * m;
// n β (n Γ m) β€ μ΄μ©ν μ μλ μ¬λ < ((n + 1) Γ m)
// μΆκ°ν΄μΌ λλ μλ² μβ€
int addServerCnt = sub / m + 1;
// addServerCntλ§νΌ μλ² μΆκ°ν΄μ£ΌκΈ°
for(int j = 0; j < addServerCnt; j++) {
q.add(new Node(i, i + k)); // (μμμκ°, λλλ μκ°)
answer += 1;
}
}
return answer;
}
}