Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba31e93

Browse files
Find Median from Data Stream
1 parent c86f3c9 commit ba31e93

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

‎295-find-median-from-data-stream.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/find-median-from-data-stream/
3+
4+
The median is the middle value in an ordered integer list. If the size of the list is even,
5+
there is no middle value and the median is the mean of the two middle values.
6+
For example, for arr = [2,3,4], the median is 3.
7+
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
8+
9+
Implement the MedianFinder class:
10+
MedianFinder() initializes the MedianFinder object.
11+
void addNum(int num) adds the integer num from the data stream to the data structure.
12+
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual
13+
answer will be accepted.
14+
15+
Example 1:
16+
Input
17+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
18+
[[], [1], [2], [], [3], []]
19+
Output
20+
[null, null, null, 1.5, null, 2.0]
21+
22+
Explanation
23+
MedianFinder medianFinder = new MedianFinder();
24+
medianFinder.addNum(1); // arr = [1]
25+
medianFinder.addNum(2); // arr = [1, 2]
26+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
27+
medianFinder.addNum(3); // arr[1, 2, 3]
28+
medianFinder.findMedian(); // return 2.0
29+
30+
Constraints:
31+
-105 <= num <= 105
32+
There will be at least one element in the data structure before calling findMedian.
33+
At most 5 * 104 calls will be made to addNum and findMedian.
34+
35+
Follow up:
36+
If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
37+
If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your
38+
solution?
39+
"""
40+
import heapq
41+
42+
43+
class MedianFinder:
44+
45+
def __init__(self):
46+
self.min_heap = []
47+
self.max_heap = []
48+
49+
50+
# Time Complexity: O(logn)
51+
def addNum(self, num: int) -> None:
52+
heapq.heappush(self.max_heap, -num)
53+
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
54+
if len(self.min_heap) > len(self.max_heap):
55+
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
56+
57+
58+
# Time Complexity: O(1)
59+
def findMedian(self) -> float:
60+
if len(self.max_heap) > len(self.min_heap):
61+
return -self.max_heap[0]
62+
63+
return (-self.max_heap[0] + self.min_heap[0]) / 2
64+
65+
66+
67+
# Your MedianFinder object will be instantiated and called as such:
68+
# obj = MedianFinder()
69+
# obj.addNum(num)
70+
# param_2 = obj.findMedian()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /