Leetcode 295. Find Median from Data Stream Intuition + Code C++ Example

preview_player
Показать описание
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.

For example, for arr = [2,3,4], the median is 3.
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
Implement the MedianFinder class:

MedianFinder() initializes the MedianFinder object.
void addNum(int num) adds the integer num from the data stream to the data structure.
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.


Example 1:

Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]

Explanation
MedianFinder medianFinder = new MedianFinder();


Constraints:

There will be at least one element in the data structure before calling findMedian.
At most 5 * 104 calls will be made to addNum and findMedian.
Рекомендации по теме
Комментарии
Автор

whenever i am stuck in any question this is the only channel which i trust.

chetanmittal
Автор

hats off👌👌, for showing your mistake in video, otherwise many youtubers only edit mistake part and only show successful thing

Auto_Enthusiast
Автор

I think when we have 1, 3, 4 in the list, the median would be 3. I'm guessing you said (1+3)/ 2 by mistake here Alisha!

pranamyavadlamani
Автор

Thank you so much for such a great explanation 🙂🙂

deepakheerakari
Автор

thanks for very descriptive explaination

hex-tech
Автор

thanks didi for such a nice editorial on this problem

supreetsingh
Автор

I love your explanation, really helpful!!

codingkart
Автор

Thank you for the video, it is very helpful!

avneetchugh
Автор

You mentioned that a heap is a complete binary tree. A binary tree takes n log n to create while a heap takes n. If a heap took n log n then we would have no benefit of using the heap in this situation. Does that sound reasonable?

avneetchugh
Автор

Aptitude drive is urs or some youtubers?
Send the link or name of youtuber

rrichard
Автор

the way of your teaching and you very nice and beauty girlll

gokulnath
Автор

class MedianFinder {
public:
MedianFinder() {

}
priority_queue<int>maxheap;
priority_queue<int, vector<int>, greater<int>>minheap;

void addNum(int num) {

maxheap.push(num);
else{
if(num>maxheap.top())
minheap.push(num);
else
maxheap.push(num);
}
int m=maxheap.size();
int n=maxheap.size();
if(m-n==2||m-n==-2){
if(m>n){
int k=maxheap.top();
maxheap.pop();
minheap.push(k);
}else{
int k=minheap.top();
minheap.pop();
maxheap.push(k);
}
}
}


double findMedian() {
int m=maxheap.size();
int n=maxheap.size();

if((m+n)%2==0)
return
if(m>n)
return maxheap.top();
else
return minheap.top();
}
};

/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
please tell me what is wrong in the above code..

jayasatwik