Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MedianFinder {
- PriorityQueue<Integer> minHeap = new PriorityQueue<>();
- PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
- public MedianFinder() {}
- public void addNum(int num) {
- maxHeap.offer(num);
- minHeap.offer(maxHeap.poll());
- if (minHeap.size() > maxHeap.size()) {
- maxHeap.offer(minHeap.poll());
- }
- }
- public double findMedian() {
- if (maxHeap.size() > minHeap.size()) {
- return maxHeap.peek();
- }
- return (minHeap.peek() + maxHeap.peek()) / 2.0d;
- }
- }
- /**
- * Your MedianFinder object will be instantiated and called as such:
- * MedianFinder obj = new MedianFinder();
- * obj.addNum(num);
- * double param_2 = obj.findMedian();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement