0
Leetcode Problem 295. Find Median from Data Stream
295. Find Median from Data Stream
AI Mock Interview
Leetcode Solutions
Approach: Two Heaps
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize two heaps: a max-heap
lo
and a min-heap
hi
.
When adding a number
num
, insert it into
lo
.
Balance the heaps by moving the top element from
lo
to
hi
.
If
hi
has more elements than
lo
, move the top element from
hi
to
lo
.
To find the median, check the size of the heaps:
If the heaps have equal size, the median is the average of the tops of both heaps.
If
lo
has one more element, the median is the top of
lo
.
Approach: Simple Sorting
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...
Sign in with LinkedIn
Sign in with Github
OR
Sign in with Email link