Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Find Median from Data Stream

Posted on April 3, 2018July 26, 2020 by braindenny

Find Median from Data Stream



Similar Problems:

  • Median of Two Sorted Arrays
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: getmedian, oodesign

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:

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?

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/find-median-from-data-stream
## Basic Ideas: Use 2 heaps: 
##      maxheap for the first half, minheap for the second half
##
##  Notice: When taking new values, we can't appeneding them directly.
##          It will not be an ordred integer list then.
##          What if we have duplicate values
##
## Complexity: Time O(log(n)), Space O(n)
import heapq
class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.left_q, self.right_q = [], []
        heapq.heapify(self.left_q)
        heapq.heapify(self.right_q)

    def addNum(self, num):
        """
        :type num: int
        :rtype: void
        """
        if len(self.left_q) == 0:
            heapq.heappush(self.left_q, -num)
            return

        if num <= -self.left_q[0]:
            # should insert to left
            heapq.heappush(self.left_q, -num)
            # rebalancing
            if len(self.left_q) > len(self.right_q)+1:
                element = -heapq.heappop(self.left_q)
                heapq.heappush(self.right_q, element)
        else:
            heapq.heappush(self.right_q, num)
            # rebalancing
            if len(self.right_q) > len(self.left_q):
                new_element = -heapq.heappop(self.right_q)
                heapq.heappush(self.left_q, new_element)

    def findMedian(self):
        """
        :rtype: float
        """
        if len(self.left_q) == 0: return None
        if (len(self.left_q) == len(self.right_q)):
            return (-self.left_q[0] + self.right_q[0])/2
        else:
            return float(-self.left_q[0])

# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
linkedin
github
slack

Post Views: 4
Posted in AmusingTagged #inspiring, getmedian, oodesign

Post navigation

LeetCode: Sort Transformed Array
LeetCode: Kth Smallest Element in a BST

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.