-
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 + "๋ฒ์งธ์ ์ถ๋ ฅ๋ฉ๋๋ค.");
}
}