-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.js
50 lines (43 loc) · 1.57 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
function solution(operations) {
const maxH = new Heap(), minH = new Heap();
const deletedMin = [], deletedMax = [];
const isEmpty = (type) => {
if (type === 'MAX') {
return maxH.size() - deletedMin.length === 0
} else {
return minH.size() - deletedMax.length === 0;
}
}
operations.forEach((operation) => {
const [op, operand] = operation.split(' ');
switch (op) {
case 'I':
maxH.insert(+operand);
minH.insert(-1 * +operand);
break;
case 'D':
if (operand > 0 && !isEmpty('MAX')) {
deletedMax.push(maxH.pop());
} else if (operand < 0 && !isEmpty('MIN')){
deletedMin.push(minH.pop() * -1);
}
break;
}
while(true) {
const isMinTopDeleted =
deletedMax.findIndex((_) => _ === minH.peek() * - 1);
const isMaxTopDeleted =
deletedMin.findIndex((_) => _ * - 1 === maxH.peek());
if (isMinTopDeleted < 0 && isMaxTopDeleted < 0) break;
if (isMinTopDeleted >= 0) {
minH.pop();
deletedMax.splice(isMinTopDeleted, 1);
}
if (isMaxTopDeleted >= 0) {
maxH.pop();
deletedMin.splice(isMaxTopDeleted, 1);
}
}
});
return [isEmpty('MAX') ? 0 : maxH.peek(), isEmpty('MIN') ? 0 : minH.peek() * - 1];
}