-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramming02.js
More file actions
54 lines (49 loc) · 1.16 KB
/
programming02.js
File metadata and controls
54 lines (49 loc) · 1.16 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
class Node{
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
}
enqueue(newValue) {
const newNode = new Node(newValue);
if (this.head === null) {
this.head = this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
}
dequeue() {
const value = this.head.value;
this.head = this.head.next;
return value;
}
peek() {
return this.head.value;
}
}
function solution(priorities, location) {
const queue = new Queue();
for (let i = 0; i < priorities.length; i++) {
queue.enqueue([priorities[i], i]);
}
priorities.sort((a,b) => b-a);
let count = 0;
while(true) {
const currentValue = queue.peek();
if (currentValue[0] < priorities[count]) {
queue.enqueue(queue.dequeue());
} else {
const value = queue.dequeue();
count += 1;
if (location === value[1]) {
return count;
}
}
}
}