LeetCode 솔루션 분류

[11/12] 295. Find Median from Data Stream

컨텐츠 정보

본문

Hard
9320180Add to ListShare

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.

  • For example, for arr = [2,3,4], the median is 3.
  • For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.

Implement the MedianFinder class:

  • MedianFinder() initializes the MedianFinder object.
  • void addNum(int num) adds the integer num from the data stream to the data structure.
  • double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.

 

Example 1:

Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]

Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1);    // arr = [1]
medianFinder.addNum(2);    // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3);    // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0

 

Constraints:

  • -105 <= num <= 105
  • There will be at least one element in the data structure before calling findMedian.
  • At most 5 * 104 calls will be made to addNum and findMedian.

 

Follow up:

  • If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
Accepted
577,692
Submissions
1,122,975

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 1451 ms, faster than 36.15% of Python3 online submissions for Find Median from Data Stream.
Memory Usage: 36.4 MB, less than 21.80% of Python3 online submissions for Find Median from Data Stream.
import heapq
class MedianFinder:

    def __init__(self):
        self.small_half = []
        self.large_half = []

    def addNum(self, num: int) -> None:
        heapq.heappush(self.small_half, -1*num)
        
        if self.small_half and self.large_half and -1*self.small_half[0] > self.large_half[0]:
            heapq.heappush(self.large_half, -1*heapq.heappop(self.small_half))
        
        if len(self.small_half) > len(self.large_half) + 1:
            heapq.heappush(self.large_half, -1*heapq.heappop(self.small_half))
        
        elif len(self.small_half) < len(self.large_half):
            heapq.heappush(self.small_half, -1*heapq.heappop(self.large_half))

    def findMedian(self) -> float:
        if len(self.small_half) == len(self.large_half):
            
            return (-self.small_half[0]+self.large_half[0])/2
        else:
            return -self.small_half[0]


# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
LeetCode 솔루션 357 / 11 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 856 명
  • 오늘 방문자 5,195 명
  • 어제 방문자 6,932 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,481 명
알림 0