Skip to content

Commit 6c50b63

Browse files
author
Joseph Luce
authored
fix run times.
1 parent e365d94 commit 6c50b63

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

leetcode/hard/295_find_median_from_data_stream.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# 295. Find Median from Data Stream
22

33
## Sort solution
4-
- Runtime: O(log(N)) for addNum() and O(N) for findMedian(), in total O(Nlog(N))
4+
- Runtime: O(N) for addNum() and O(1) for findMedian(), in total O(N\*N) worst case)
55
- Space: O(N)
66
- N = Number of elements in array
77

88
This solution is fairly simple, as long as we keep a sorted order of numbers, we can figure out the median quickly.
99

1010
However, it is important to note that if we instead did a different approach where we would add the value into the list then sort it only when findMedian() was called, it would actually cause a slower run-time, O(N * Nlog(N)).
1111
The worst case is when we call findMedian() after each newly inserted number.
12+
We would basically be sorting the entire array N times.
1213
That is because every time we would sort, the list keeps growing, we aren't utilizing the fact that the list is already sorted.
1314
With an already sorted list, we can just perform a binary search and insert the new number instead of resorting an already sorted list for each number.
1415

@@ -35,7 +36,7 @@ class MedianFinder:
3536
```
3637

3738
## Two Heap Solution
38-
- Runtime: O(log(N)) for addNum() and O(1) for findMedian(), in total O(Nlog(N))
39+
- Runtime: O(log(N)) for addNum() and O(1) for findMedian(),in total O(Nlog(N)) worst case
3940
- Space: O(N)
4041
- N = Number of elements in array
4142

0 commit comments

Comments
 (0)