-
Notifications
You must be signed in to change notification settings - Fork 0
/
running-median.ts
97 lines (71 loc) · 1.88 KB
/
running-median.ts
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
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString: string = '';
let inputLines: string[] = [];
let currentLine: number = 0;
process.stdin.on('data', function (inputStdin: string): void {
inputString += inputStdin;
});
process.stdin.on('end', function (): void {
inputLines = inputString.split('\n');
inputString = '';
main();
});
function readLine(): string {
return inputLines[currentLine++];
}
class MedianHeap {
public heap: Array<number>;
constructor() {
this.heap = [];
}
insert(value: number) {
this.heap.push(value);
let index = this.heap.length - 1; // 2
const current = this.heap[index]; // 5
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2); // 0
let parent = this.heap[parentIndex]; // 12
if (parent > current) {
this.heap[parentIndex] = current;
this.heap[index] = parent;
index = parentIndex;
} else {
break;
}
}
}
}
/*
* Complete the 'runningMedian' function below.
*
* The function is expected to return a DOUBLE_ARRAY.
* The function accepts INTEGER_ARRAY a as parameter.
*/
function runningMedian(a: number[]): number[] {
const results = [];
const tree = new MedianHeap();
for (let i = 0; i < a.length; i++) {
tree.insert(a[i]);
console.log(tree);
console.log('\n');
const mid = Math.floor(tree.heap.length / 2);
if (tree.heap.length % 2 == 0) {
results.push(tree.heap[mid]);
} else {
results.push((tree.heap[mid] + tree.heap[mid + 1]) / 2);
}
}
return results;
}
function main() {
const aCount: number = parseInt(readLine().trim(), 10);
let a: number[] = [];
for (let i: number = 0; i < aCount; i++) {
const aItem: number = parseInt(readLine().trim(), 10);
a.push(aItem);
}
const result: number[] = runningMedian(a);
console.log(result);
}