295.Find-Median-from-Data-Stream

295. Find Median from Data Stream

题目地址

https://leetcode.com/problems/find-median-from-data-stream/

https://www.lintcode.com/problem/find-median-from-data-stream

题目描述

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.

For example,
[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.

Example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2

Follow up:

If all integer numbers from the stream are between 0 and 100, how would you optimize it?
If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?

代码

Approach 1: Simple Sorting

Time complexity: O(nlogn) + O(1)O(nlogn)

  • Adding a number takes amortized O(1) time for a container with an efficient resizing scheme.

  • Finding the median is primarily dependent on the sorting that takes place. This takes O(nlogn) time for a standard comparative sort.

class MedianFinder {
    ArrayList<Integer> store;

    public MedianFinder() {
       store = new ArrayList<Integer>();
    }

    public void addNum(int num) {
      store.add(num);
    }

    public double findMedian() {
      Collections.sort(store);
      int n = store.size();
      if (n % 2 == 1) {
        return store.get(n / 2);
      } else {
        return (store.get(n / 2 - 1) + store.get(n / 2)) * 0.5;
      }
    }
}

Approach #2 Two heap

  • Max-heap small has the smaller half of the numbers.

  • Min-heap large has the larger half of the numbers.

Complexity Analysis

  • Time complexity: O(5⋅logn) + O(1) ≈ O(logn)

    • At worst, there are three heap insertions and two heap deletions from the top. Each of these takes about O(logn) time.

    • Finding the mean takes constant O(1) time since the tops of heaps are directly accessible.

  • Space complexity: O(n) linear space to hold input in containers.

class MedianFinder {
    private Queue<Long> small = new PriorityQueue(Collections.reverseOrder()),
                        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;
    }
};
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;
    }
};

Last updated