-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11279.js
86 lines (66 loc) · 1.86 KB
/
11279.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
/*
힙 구현은 https://github.com/python/cpython/blob/main/Lib/heapq.py 여기 참고함
*/
class MaxHeap {
list = [null];
getLength() {
return this.list.length - 1;
}
push(newValue) {
this.list.push(newValue);
this.dragParentDown(this.getLength(), 1);
}
pop() {
if (!this.getLength()) throw new Error('Heap Empty');
const deleteValue = this.list[1];
const lastValue = this.list.pop();
if (this.getLength()) {
this.list[1] = lastValue;
this.pullChildUp(1);
}
return deleteValue;
}
dragParentDown(startPos, finalParentPos) {
const oddball = this.list[startPos];
let current;
for (
current = startPos;
current > finalParentPos && oddball > this.list[Math.floor(current / 2)];
current = Math.floor(current / 2)
) {
this.list[current] = this.list[Math.floor(current / 2)];
}
this.list[current] = oddball;
}
pullChildUp(startPos) {
const oddball = this.list[startPos];
let current = startPos;
while (current * 2 <= this.getLength()) {
const leftChild = current * 2;
const rightChild = leftChild + 1;
const largerChild = (
rightChild <= this.getLength() && this.list[leftChild] < this.list[rightChild]
? rightChild
: leftChild
);
this.list[current] = this.list[largerChild];
current = largerChild;
}
this.list[current] = oddball;
this.dragParentDown(current, startPos);
}
}
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [, ...commands] = require('fs').readFileSync(INPUT_FILE).toString().trim()
.split('\n')
.map(Number);
const maxHeap = new MaxHeap();
const sol = [];
commands.forEach((value) => {
if (value) {
maxHeap.push(value);
return;
}
sol.push(maxHeap.getLength() ? maxHeap.pop() : 0);
});
console.log(sol.join('\n'));