-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathFind Median from Data Stream.js
141 lines (117 loc) · 3.02 KB
/
Find Median from Data Stream.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
/**
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:
[2,3,4] , the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.
For example:
add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
*/
// SOLUTION 1: use a max heap and min heap
// java solution
/**
class MedianFinder {
private Queue<Long> small = new PriorityQueue(),
large = new PriorityQueue();
public void addNum(int num) {
large.add((long) num);
small.add(-large.poll());
if (large.size() < small.size())
large.add(-small.poll());
}
public double findMedian() {
return large.size() > small.size()
? large.peek()
: (large.peek() - small.peek()) / 2.0;
}
};
*/
// SOLUTION 2: BST
/**
* My BST node, has extra attribute initialized as 0
*/
class BSTNode {
constructor (val) {
this.val = val;
this.left = null;
this.right = null;
this.size = 1;
}
}
class BST {
constructor() {
this.root = null;
}
add(val, node) {
if (!node) {
this.root = new BSTNode(val);
return;
}
if (val > node.val) {
if (node.right) {
this.add(val, node.right);
} else {
node.right = new BSTNode(val);
}
} else {
if (node.left) {
this.add(val, node.left);
} else {
node.left = new BSTNode(val);
}
}
node.size++;
}
rank(k) {
let node = this.root;
while(true) {
const leftSize = node.left ? node.left.size : 0;
if (leftSize === k) {
return node.val;
}
if (leftSize > k) {
node = node.left;
} else {
node = node.right;
k = k - leftSize - 1;
}
}
}
}
/**
* @constructor
*/
var MedianFinder = function() {
this.BST = new BST();
};
/**
* @param {integer} word
* @return {void}
* Adds a num into the data structure.
*/
MedianFinder.prototype.addNum = function(num) {
this.BST.add(num, this.BST.root);
};
/**
* @return {double}
* Return median of current data stream
*/
MedianFinder.prototype.findMedian = function() {
const size = this.BST.root.size;
if (size % 2 === 0) {
return (this.BST.rank(size / 2) + this.BST.rank(size / 2 - 1)) / 2;
}
return this.BST.rank(parseInt(size / 2));
};
/**
* Your MedianFinder object will be instantiated and called as such:
* var mf = new MedianFinder();
* mf.addNum(1);
* mf.findMedian();
*/