-
Notifications
You must be signed in to change notification settings - Fork 0
/
프린터.js
64 lines (58 loc) · 1.43 KB
/
프린터.js
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
/*
기본적인 큐의 기능 외에
큐를 한 번 순환하면서 중요도를 확인하는 기능 필요
*/
class Node {
constructor(priority, location) {
this.priority = priority;
this.location = location;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
enqueue(priority, location) {
const newNode = new Node(priority, location);
if (this.length === 0) {
this.first = newNode;
} else {
this.last.next = newNode;
}
this.last = newNode;
this.length += 1;
}
dequeue() {
const { priority, location, next } = this.first;
this.length -= 1;
if (this.length === 0) {
this.last = null;
}
this.first = next;
return { priority, location };
}
hasMoreImportantDocument(priority) {
for (let cur = this.first; cur !== null; cur = cur.next) {
if (cur.priority > priority) return true;
}
return false;
}
}
const solution = (priorities, targetLocation) => {
const queue = new Queue();
priorities.forEach((priority, index) => { queue.enqueue(priority, index); });
let printTime = 0;
while (queue.length > 0) {
const { priority, location } = queue.dequeue();
if (queue.hasMoreImportantDocument(priority)) {
queue.enqueue(priority, location);
} else {
printTime += 1;
if (location === targetLocation) return printTime;
}
}
return -1;
};