aboutsummaryrefslogtreecommitdiff
path: root/find_median_data_stream.cpp
blob: 14fbcc320e7132bc29adeb6e80454d0382c3dda5 (plain)
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
#include "leetcode.h"

class MedianFinder {
public:
  void addNum(int num) {
    small.push(num);
    large.push(-small.top());
    small.pop();
    if (small.size() < large.size()) {
      small.push(-large.top());
      large.pop();
    }
  }
  double findMedian() {
    if (small.size() > large.size())
      return small.top();
    else
      return (small.top() - large.top()) / 2.0;
  }

private:
  priority_queue<long> small, large;
};

int main() {
  MedianFinder *obj = new MedianFinder();
  obj->addNum(69);
  double param_2 = obj->findMedian();
}