-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.js
157 lines (139 loc) · 3.39 KB
/
Solution.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class FrontMiddleBackQueue {
constructor() {
this.q1 = new Deque();
this.q2 = new Deque();
}
pushFront(val) {
this.q1.pushFront(val);
this.rebalance();
}
pushMiddle(val) {
this.q1.pushBack(val);
this.rebalance();
}
pushBack(val) {
this.q2.pushBack(val);
this.rebalance();
}
popFront() {
if (this.q1.isEmpty() && this.q2.isEmpty()) {
return -1;
}
const val = this.q1.isEmpty() ? this.q2.popFront() : this.q1.popFront();
this.rebalance();
return val !== undefined ? val : -1;
}
popMiddle() {
if (this.q1.isEmpty() && this.q2.isEmpty()) {
return -1;
}
const val =
this.q1.getSize() === this.q2.getSize() ? this.q1.popBack() : this.q2.popFront();
this.rebalance();
return val !== undefined ? val : -1;
}
popBack() {
if (this.q2.isEmpty()) {
return -1;
}
const val = this.q2.popBack();
this.rebalance();
return val !== undefined ? val : -1;
}
rebalance() {
if (this.q1.getSize() > this.q2.getSize()) {
this.q2.pushFront(this.q1.popBack());
}
if (this.q2.getSize() > this.q1.getSize() + 1) {
this.q1.pushBack(this.q2.popFront());
}
}
}
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class Deque {
constructor() {
this.front = null;
this.back = null;
this.size = 0;
}
pushFront(val) {
const newNode = new Node(val);
if (this.isEmpty()) {
this.front = newNode;
this.back = newNode;
} else {
newNode.next = this.front;
this.front.prev = newNode;
this.front = newNode;
}
this.size++;
}
pushBack(val) {
const newNode = new Node(val);
if (this.isEmpty()) {
this.front = newNode;
this.back = newNode;
} else {
newNode.prev = this.back;
this.back.next = newNode;
this.back = newNode;
}
this.size++;
}
popFront() {
if (this.isEmpty()) {
return undefined;
}
const value = this.front.value;
this.front = this.front.next;
if (this.front !== null) {
this.front.prev = null;
} else {
this.back = null;
}
this.size--;
return value;
}
popBack() {
if (this.isEmpty()) {
return undefined;
}
const value = this.back.value;
this.back = this.back.prev;
if (this.back !== null) {
this.back.next = null;
} else {
this.front = null;
}
this.size--;
return value;
}
frontValue() {
return this.front?.value;
}
backValue() {
return this.back?.value;
}
getSize() {
return this.size;
}
isEmpty() {
return this.size === 0;
}
}
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* var obj = new FrontMiddleBackQueue()
* obj.pushFront(val)
* obj.pushMiddle(val)
* obj.pushBack(val)
* var param_4 = obj.popFront()
* var param_5 = obj.popMiddle()
* var param_6 = obj.popBack()
*/