-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxQueue.js
More file actions
48 lines (43 loc) · 865 Bytes
/
maxQueue.js
File metadata and controls
48 lines (43 loc) · 865 Bytes
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
class Queue {
// Your code here
constructor() {
this.items = [];
this.count = 0;
this.max = [];
}
getLength() {
return this.count;
}
enqueue(item) {
this.items.unshift(item);
this.count = this.count + 1;
if(this.max.length === 0 || this.max[0] <= item){
this.max.unshift(item);
}
}
dequeue() {
if(this.count > 0) {
this.count = this.count - 1;
}
var item = this.items.shift();
if(item === this.max[0]){
this.max.shift();
}
if(this.max.length === 0 ){
var tempMax;
for(var i = 0; i < this.items.length; i++){
if(tempMax < this.items[i]){
tempMax = this.item[i];
}
}
this.max[0] = tempMax;
}
return item;
}
peek() {
return this.items.slice(0,1)[0];
}
getMax() {
return this.max[0];
}
};