You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/hard/295_find_median_from_data_stream.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# 295. Find Median from Data Stream
2
2
3
3
## Sort solution
4
-
- Runtime: O(Nlog(N)) for addNum() and O(1) for findMedian()
4
+
- Runtime: O(log(N)) for addNum() and O(1) for findMedian(), in total O(Nlog(N))
5
5
- Space: O(N)
6
6
- N = Number of elements in array
7
7
@@ -35,7 +35,7 @@ class MedianFinder:
35
35
```
36
36
37
37
## Two Heap Solution
38
-
- Runtime: O(Nlog(N)) for addNum() and O(1) for findMedian()
38
+
- Runtime: O(log(N)) for addNum() and O(1) for findMedian(), in total O(Nlog(N))
39
39
- Space: O(N)
40
40
- N = Number of elements in array
41
41
@@ -46,6 +46,8 @@ You can think of this as a divide and conquer approach.
46
46
The only tricky part is to keep the two heaps balanced, balanced meaning the two heaps cannot differ in size by more than 1.
47
47
Secondly, we need to keep the two heap property of smaller and larger sets.
48
48
49
+
Once these two properties are met, finding the median can be done by using the two values on top of the heap if both heap sizes are the same or taking the top value of the larger heap.
0 commit comments