bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 295. Find Median from Data Stream

295. Find Median from Data Stream

Leetcode Solutions

Approach: Two Heaps

  1. Initialize two heaps: a max-heap lo and a min-heap hi.
  2. When adding a number num, insert it into lo.
  3. Balance the heaps by moving the top element from lo to hi.
  4. If hi has more elements than lo, move the top element from hi to lo.
  5. 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.
UML Thumbnail

Approach: Simple Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR