-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrinter.java
More file actions
52 lines (42 loc) Β· 1.73 KB
/
Printer.java
File metadata and controls
52 lines (42 loc) Β· 1.73 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.*;
public class PrinterQueue {
public int solution(int[] priorities, int location) {
// 1. λ¬Έμλ€μ (μ°μ μμ, μΈλ±μ€) ννλ‘ νμ μ μ₯
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < priorities.length; i++) {
queue.add(new int[]{priorities[i], i}); // [μ°μ μμ, μΈλ±μ€]
}
int count = 0; // μΆλ ₯λ λ¬Έμ μλ₯Ό μΈλ λ³μ
while (!queue.isEmpty()) {
int[] current = queue.poll(); // 맨 μ λ¬Έμλ₯Ό κΊΌλ
// 2. νμ νμ¬λ³΄λ€ λ λμ μ°μ μμκ° μλμ§ κ²μ¬
boolean hasHigherPriority = false;
for (int[] doc : queue) {
if (doc[0] > current[0]) { // λ λμ μ°μ μμκ° μλ€λ©΄
hasHigherPriority = true;
break;
}
}
if (hasHigherPriority) {
// 3. λ μ€μν λ¬Έμκ° μμΌλ©΄ λ€μ μ€ λ€λ‘ 보λ
queue.add(current);
} else {
// 4. μΆλ ₯ κ°λ₯νλ©΄ count μ¦κ°
count++;
// 5. μΆλ ₯ν λ¬Έμκ° λ΄κ° μ°Ύλ λ¬Έμλ©΄ count λ°ν
if (current[1] == location) {
return count;
}
}
}
return -1; // μ΄λ‘ μ μ λ λλ¬νμ§ μμ
}
// μ€ν μ½λ μμ
public static void main(String[] args) {
PrinterQueue pq = new PrinterQueue();
int[] priorities = {2, 1, 3, 2};
int location = 2;
int result = pq.solution(priorities, location);
System.out.println("λ΄ λ¬Έμλ " + result + "λ²μ§Έμ μΆλ ₯λ©λλ€.");
}
}