Find Median from Data Stream

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

ความคิดเห็น • 142

  • @atishaysrivastava7772
    @atishaysrivastava7772 3 ปีที่แล้ว +55

    Hey thanks for uploading, I have requested this video in previous video and yesterday I got intern offer from Intuit. Thanks to you ❤️.
    Everything you explain are too good to grab and follow-up.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +5

      Woww ❤️ congratulations 🎉

    • @hardikagarwal7652
      @hardikagarwal7652 3 ปีที่แล้ว +1

      Bhai kaise mili intuit m intern?

    • @jettdexter6719
      @jettdexter6719 3 ปีที่แล้ว

      i guess Im randomly asking but does any of you know a way to log back into an instagram account?
      I was dumb forgot the login password. I would love any help you can give me!

    • @CodeSuccessChronicle
      @CodeSuccessChronicle 3 ปีที่แล้ว +4

      @@jettdexter6719 try forget password dude

    • @pranav288
      @pranav288 ปีที่แล้ว

      @@CodeSuccessChronicle it's a bot

  • @ashishkumarchoubey5592
    @ashishkumarchoubey5592 3 ปีที่แล้ว +33

    For the first time in almost 6 months finally we see a face behind the voice that has been guiding me in coding.

  • @Mr_SSK
    @Mr_SSK 2 ปีที่แล้ว +9

    *THE IMPLEMENTATION CAN BE MADE A LOT MORE SIMPLIER- *
    class MedianFinder {
    public:
    priority_queue maxheap; //1st half -> in case odd size of the total stream, the extra ele will be in the left half (max-heap)
    priority_queue minheap; //2nd half
    MedianFinder() {
    }
    void addNum(int num) {
    int lsize = maxheap.size();
    int rsize = minheap.size();
    if(lsize==0) maxheap.push(num); // the right-half is surely empty -> so, num is the 1st element in stream -> store it in the first half
    else if(lsize==rsize) { // as the max-heap can take one extra element -> So, ONE element will go on first half (BUT, NOT NECESSARILY 'NUM')
    if(num that means lsize>rsize. So, one element will surely be inserted in right side (BUT, NOT NECESSARILY 'NUM')
    if(num>maxheap.top()) minheap.push(num); // when num>maxHeap.top(), it will obviously go on the right-side -> No Violation
    else{ // Otherwise, we need to shift the maxHeap.top() to the minHeap, and push the 'num' in maxHeap
    int temp = maxheap.top(); maxheap.pop();
    maxheap.push(num);
    minheap.push(temp);
    }
    }
    }

    double findMedian() {
    int totSize = maxheap.size() + minheap.size();
    return totSize%2==1? maxheap.top() : (maxheap.top()+minheap.top())/2.0;
    }
    };
    EXPLANATION IN SIMPLE WORDS-
    -> We divide the entire array into two halves, and there can be two cases -
    - In case of ODD size, the maximum element from the left side will be the mediun (as we keep one extra element in the left side)
    - In case of EVEN size, the average of 'maximum from left' and 'minimum from right' will be the mediun
    -> In order to maintain the max and min from both the sides respectively we can use maxHeap for the left-side (as the root will hold the max),
    and minHeap for the right-side (as the root will hold the min)
    BUT, WHEN TO INSERT IN WHICH SIDE?
    -> There can be 3 major cases-
    1. When both the sides are empty - Insertion will take place in left side for sure
    2. When both the sides have equal elements (but, not 0) - One element will surely be inserted in left-side as we keep one extra element in
    left side, BUT not necesarily the current element. There will be 2 cases-
    - when the current element is lesser than the minimum of right-side, the current element will surely be inserted in left-side, as there's
    NO VIOLATION
    - Otherwise, shift the minHeap.top() to the maxHeap, and push the 'num' in minHeap (to maintain the overall ordering)
    3. When left-side already have one extra element - in this case one element will surely be inserted in right-side, BUT not necessarily the
    current one. There will be 2 cases-
    - if the current element is greater than the maximum of left-side, then obviously the current element will be placed in the right-side (No
    Violation)
    - Otherwise, we need to shift the maxHeap.top() to the minHeap, and push the 'num' in maxHeap
    TC: O(N*logN), as it takes O(logN) for the operations in HEAP
    SC: O(N), as we store the elements in HEAP*/
    ANYWAYS, AMAZINGGGGGGGGGGGGG EXPLANATION....

    • @anu8928
      @anu8928 10 หลายเดือนก่อน

      Very Nicely explained!! 🙏🏼

    • @PraveenKumar-vv6mh
      @PraveenKumar-vv6mh หลายเดือนก่อน

      yes, it can be simplified as you said, even i also have thought the same.
      These videos are really useful and good explanation for all the possible cases.
      while inserting the element into min or max heap, check the below conditions.
      o if max heap size is 0, then insert the element into max heap
      o if max heap and min heap size are same, then
       if num < max heap root then
      • insert into max heap
       if num > max heap root then
      • pop the min heap root
      • insert that popped one into max heap
      • insert the num into min heap
      o else [ if max heap = 1 + min heap size, no need to check ]
       num < max heap root
      • pop the max heap root
      • insert the popped one into min heap
      • insert the num into max heap
       num > max heap root
      • insert into min heap
      o if max heap < min heap size [ this will never be the case ]

  • @lalit2926
    @lalit2926 ปีที่แล้ว +1

    For the first time, I came to know about real life use of insertion sort.. Thank you Sir !! Great explaintion...

  • @NatureLover-oq6uc
    @NatureLover-oq6uc ปีที่แล้ว

    Mujhe nhi lagta TH-cam pe Striver aur TechDose se jyada achaa coding ke liye koi channel h........YOU ARE THE GEM❤

  • @nipunaggarwal7568
    @nipunaggarwal7568 3 ปีที่แล้ว +7

    Just completed this Heap Series. You are just amazing. Recommended your channel to all my friends. Sir, is deque important for interviews? If so, can you also please make a series on deque also?

  • @abhisheksrivastav9152
    @abhisheksrivastav9152 ปีที่แล้ว

    Thank you for this nice explanation. I visited different sites to solve this question. Your solution is very simple and efficient to understand.

  • @theghostwhowalk
    @theghostwhowalk 3 ปีที่แล้ว +7

    Great explanation to this hard problem. Lot of companies ask this one.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Yea. It's very important :)

    • @dhruvkaran9724
      @dhruvkaran9724 2 ปีที่แล้ว

      @@techdose4u amazon asked me this and i was totally clueless now i know y i was clueless

  • @Rahul-rp5hk
    @Rahul-rp5hk 3 ปีที่แล้ว +3

    Amazing explanation! It would have helped even more if you could discuss the follow up in the question as well!

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +2

      Hmmm....I somehow missed the follow-up I guess :O

  • @aadityasharma6855
    @aadityasharma6855 ปีที่แล้ว

    Really amazing explaination, hands down one of the best dsa teacher on yt.

  • @twi4458
    @twi4458 ปีที่แล้ว

    Best Explaination on youtube!

  • @minhthuan4479
    @minhthuan4479 2 ปีที่แล้ว

    2 days to complete this course. Thank you for your valuable lesson.

  • @aryanmaniyar7301
    @aryanmaniyar7301 11 หลายเดือนก่อน

    Great video, and great questions as well during the switch from brute to optimal! Loved it, thanks :)

  • @VijaySharma-qb2rl
    @VijaySharma-qb2rl 3 ปีที่แล้ว

    This channel deserves 1M subs!

  • @gandhijainamgunvantkumar6783
    @gandhijainamgunvantkumar6783 2 ปีที่แล้ว +3

    I think in the else part of the addNum function, we don't have to write that many conditions, as we know that the maxheap size is 1 greater than minheap size, hence we must insert the element such that after the insertion maxheap.size() will be same as minheap.size().
    So here we just have to check whehter num is less than or equal (

    • @manishkumaryadav4303
      @manishkumaryadav4303 ปีที่แล้ว

      we can make it even shorter....
      .
      void addNum(int num) {
      int l=maxh.size(),r=minh.size();
      if(l==r) maxh.push(num);
      else minh.push(num);
      if(r!=0)
      {
      if(maxh.top()>minh.top())
      {
      int a=maxh.top(),b=minh.top();
      maxh.pop();
      minh.pop();
      maxh.push(b);
      minh.push(a);
      }
      }
      }

  • @pepetikesavasiddhardha7852
    @pepetikesavasiddhardha7852 2 ปีที่แล้ว +1

    Thanks for the wonderful explination

  • @DeepakKumar-yc9hr
    @DeepakKumar-yc9hr 3 ปีที่แล้ว +1

    No words for you , only I want to say ......................You are Diamond .......................

  • @anikkhan8811
    @anikkhan8811 3 ปีที่แล้ว +7

    Really nice man! The questions you put to improve over the brute force algo are really crucial to understand the intuition. Thanks Sir. Plz consider doing more leetcode :)

  • @ismail8973
    @ismail8973 3 ปีที่แล้ว +2

    Watched the entire heap series. can i hope for more series like this

  • @AR-scorp
    @AR-scorp 2 ปีที่แล้ว

    Awesome. Just when you said devide them jnto 2 halve, immediately it became easier.

  • @vaishnavichilukuri2214
    @vaishnavichilukuri2214 3 ปีที่แล้ว +1

    Why can't we use binary search to insert?
    It will also work with same nlogn complexity, right?

  • @harryscorner7135
    @harryscorner7135 3 ปีที่แล้ว +1

    We can simply use binary search for each element for insertion right?? like bisect. Correct me if I am wrong / missing out something.

    • @harryscorner7135
      @harryscorner7135 3 ปีที่แล้ว +1

      Thank you for this Amazing Video Sir

  • @paragroy5359
    @paragroy5359 2 ปีที่แล้ว +1

    Nice explanation sir...thanks a lot for the video

  • @tushargupta5805
    @tushargupta5805 3 ปีที่แล้ว +1

    I'm the 250th like. you deserve more keep it up

  • @yitingg7942
    @yitingg7942 3 ปีที่แล้ว +2

    Hi Surya, May I ask How to answer before follow ups for this question?
    If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
    If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Let me check the follow ups. Dint see that to be honest.

    • @sarthuaksharma9609
      @sarthuaksharma9609 3 ปีที่แล้ว +2

      Bucket sort is one way for small range questions. Keep 100 buckets for all the values for the second follow up keep 101 buckets one extra bucket for all the values greater than 100 keep them sorted as they are just 1% sorting them won't be costly. Read about bucket sort and try to apply here you will get the intuition.

  • @ravipatel3624
    @ravipatel3624 3 ปีที่แล้ว

    Best Explanation. Hats off you.

  • @SurajKumar-vo9jf
    @SurajKumar-vo9jf 3 ปีที่แล้ว +4

    Thank you for helping out so much, please do sql also

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Let's see after DSA course.

  • @HarendraKumar-hl8nh
    @HarendraKumar-hl8nh 2 ปีที่แล้ว +1

    What a beautiful explanation !❤️ Thank you Bhaiya again...

  • @munishgarg75
    @munishgarg75 3 ปีที่แล้ว

    You are really doing an awesome job sir. May God bless you!!!!

  • @ashishlalwani582
    @ashishlalwani582 3 ปีที่แล้ว +1

    the best solution ever !!

  • @nguyenthong3096
    @nguyenthong3096 ปีที่แล้ว

    You're my hero man.

  • @aakashparmar560
    @aakashparmar560 2 ปีที่แล้ว

    Great Explanation.!

  • @pujabhardwaj7569
    @pujabhardwaj7569 2 ปีที่แล้ว +1

    Thank you for such a great explanation

  • @taukirkhatri3368
    @taukirkhatri3368 3 ปีที่แล้ว +2

    What an amazing explanation!🤩 Hats off to you🙌🏻

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Thanks :)

    • @taukirkhatri3368
      @taukirkhatri3368 3 ปีที่แล้ว

      I don't know why your channel is not getting a proper growth!!! Such an amazing series of videos. But no one is sharing your content.

    • @taukirkhatri3368
      @taukirkhatri3368 3 ปีที่แล้ว

      Do you have an account on Instagram? You can promote it there. I will also share your videos on my profile. I know I don't have much followers but 'something is better than nothing' Right?

    • @taukirkhatri3368
      @taukirkhatri3368 3 ปีที่แล้ว

      And also can you show your profiles of Codechef or CodeForces or other coding platforms on which you are doing Competitive Programming in upcoming video? Because all the people are interested in numbers only. So it will help you to grow up your channel 🤟🏻

  • @sejalrai30
    @sejalrai30 2 ปีที่แล้ว

    very nice sir thank you so much

  • @PrashantKumar-gg5qd
    @PrashantKumar-gg5qd 2 ปีที่แล้ว +1

    Hi,thanks, i liked your explanation @17:00

  • @Young10489
    @Young10489 2 ปีที่แล้ว +1

    Great teaching!

  • @chetnaraghav9979
    @chetnaraghav9979 2 ปีที่แล้ว

    great explanation

  • @SibiRanganathL
    @SibiRanganathL หลายเดือนก่อน

    This dude is awesome

  • @Eng.RajniPaneru
    @Eng.RajniPaneru 2 ปีที่แล้ว

    thanks for your explanation, sir this is really good .

  • @alperkaya8919
    @alperkaya8919 ปีที่แล้ว

    perfect Explanation

  • @amangupta0229
    @amangupta0229 3 ปีที่แล้ว +1

    can we use quick select algorithm for findinding mid or kth element in unordered array

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      You can. But still it will be too expensive and doesn't go well with stream.

  • @hartiksuhagiya5705
    @hartiksuhagiya5705 2 หลายเดือนก่อน

    Good solution

  • @PhenomenalInitiations
    @PhenomenalInitiations 3 ปีที่แล้ว

    It took 3.01seconds for me. Not sure if I wrote a good code. Did it take the same time for you also? or even less?

  • @harshalgarg1149
    @harshalgarg1149 3 ปีที่แล้ว +1

    Thank you so much.

  • @manavjain2078
    @manavjain2078 3 ปีที่แล้ว +1

    Hey , we can use multiset as well

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      I dint try that. You can try once :)

  • @devyeag3096
    @devyeag3096 2 ปีที่แล้ว

    Why, quick sort algorithm is not used (at the position of insertion sort)

  • @shubhamdeshmukh9287
    @shubhamdeshmukh9287 3 ปีที่แล้ว

    very well, oh wait , Awesome explanation

  • @tanveer.shaikh
    @tanveer.shaikh 2 ปีที่แล้ว

    in case of else condition you have made an extra check by checking if size of min heap == 0 and then followed by condition I think that is extra not required to do that and also it is confusing

  • @mwshubham
    @mwshubham 3 ปีที่แล้ว +1

    Does practice is a only solution to get the solution of such problems? It is very hard to guess the best solution of such problems without getting hints.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      Practice obviously helps. But there are other factors like how well you understand a problem statement and how much you enjoy doing this as well as guidance.

  • @devashishvyas9608
    @devashishvyas9608 3 ปีที่แล้ว +1

    nice explanation bro keep the work going

  • @megm9963
    @megm9963 3 ปีที่แล้ว +2

    Thanks

  • @anuragagnihotri5238
    @anuragagnihotri5238 3 ปีที่แล้ว +1

    Nice explanation

  • @danielmcgee2447
    @danielmcgee2447 3 ปีที่แล้ว

    Isn't nlogn the same time complexity keeping a vector, and then sorting it at time of calculating median? wouldn't it be easier and equivalent in time complexity? also how is it possible to have maxHeap size >1 and minheap size >0, i thought your goal was to keep maxHeap greater by 1 or equal to min heap size? therefore you cannot push directly to minHeap for this scenario?

    • @HARSHITKUMAR-wj4ex
      @HARSHITKUMAR-wj4ex 3 ปีที่แล้ว +2

      no that would be n^2logn time because in that case you would be sorting n time n numbers and that would be n*(nlogn)

  • @Rajat-Sharma1
    @Rajat-Sharma1 3 ปีที่แล้ว +1

    could code own my own after the explanation. you explain really well

  • @bestsaurabh
    @bestsaurabh 3 ปีที่แล้ว

    We can directly use the underlying vector used for min heap. Since we know the heap size, we can fetch the middle element(s) in O(1). Can you please suggest what's the issue with this approach?

  • @shankhadeepbanerjee4980
    @shankhadeepbanerjee4980 3 ปีที่แล้ว

    great explaination

  • @prakharagarwal6237
    @prakharagarwal6237 3 ปีที่แล้ว

    Thanks! Keep up the good work!😃

  • @akshatshah6413
    @akshatshah6413 3 ปีที่แล้ว

    Can we use multiset here

  • @bhargav8263
    @bhargav8263 2 ปีที่แล้ว

    excellent

  • @tirthjayswal9895
    @tirthjayswal9895 3 ปีที่แล้ว +1

    Good explenation

  • @SugamMaheshwari
    @SugamMaheshwari 3 ปีที่แล้ว +1

    Beautiful question and superb explanation ❤

  • @poojitharavuri2943
    @poojitharavuri2943 3 ปีที่แล้ว

    Sir please do a video on this problem: Median of Two Sorted arrays

  • @salbyrones3075
    @salbyrones3075 2 ปีที่แล้ว

    Just a feedback that, In the last else condition, we don't need so many conditions

  • @amansiddiqui3218
    @amansiddiqui3218 2 ปีที่แล้ว

    if still anyone having problem to understand this, i'll highly recommend you to watch Keerti Purswami's video. You will definitely understand and also the code is too short compared to this one.

  • @PRANAVMAPPOLI
    @PRANAVMAPPOLI 3 ปีที่แล้ว +1

    Nice❣️

  • @11shivamAhir00
    @11shivamAhir00 2 ปีที่แล้ว

    thank u sir

  • @shubhamdeshmukh9287
    @shubhamdeshmukh9287 3 ปีที่แล้ว

    amazing , smooth

  • @hakeeminfosec
    @hakeeminfosec ปีที่แล้ว

    thanks sir

  • @somurauniyar2610
    @somurauniyar2610 3 ปีที่แล้ว

    nice explaination sir.... but the code is too complex to understand

  • @vanshsindhu8559
    @vanshsindhu8559 3 ปีที่แล้ว +1

    Sir I like your face Cam. 😍
    But It would be great, if increase size of your face and have green screen at ur back.
    Thanks Sir

  • @jheelgala6353
    @jheelgala6353 ปีที่แล้ว

    what if we use avl tree??

  • @sunilkumawat8787
    @sunilkumawat8787 2 ปีที่แล้ว +1

    My according median for odd number is (n+1)/2

  • @santanudutta3809
    @santanudutta3809 3 ปีที่แล้ว +1

    Leetcode 480 (Sliding Window Median), please !!

  • @jagadeeshp2558
    @jagadeeshp2558 3 ปีที่แล้ว +1

    Waiting for hashing and recursion ❤️

  • @acxd
    @acxd ปีที่แล้ว

    love babbar has explained this better

  • @dhruvkaran9724
    @dhruvkaran9724 2 ปีที่แล้ว

    supeeeeeeerb

  • @sayantaniguha8519
    @sayantaniguha8519 3 ปีที่แล้ว +1

    hum n^(2)logn approach se dil todke iha pe ata hai

  • @tanveer.shaikh
    @tanveer.shaikh 2 ปีที่แล้ว

    I feel last condition is extra

  • @Sambob006
    @Sambob006 3 ปีที่แล้ว +1

    Question 295 has 295 likes right now lol!

  • @dakshsingh5891
    @dakshsingh5891 2 หลายเดือนก่อน

    this one is hard.

  • @manishkumaryadav4303
    @manishkumaryadav4303 ปีที่แล้ว

    add function can be made shorter
    void addNum(int num) {
    int l=maxh.size(),r=minh.size();
    if(l==r) maxh.push(num);
    else minh.push(num);
    if(r!=0)
    {
    if(maxh.top()>minh.top())
    {
    int a=maxh.top(),b=minh.top();
    maxh.pop();
    minh.pop();
    maxh.push(b);
    minh.push(a);
    }
    }
    }

  • @soumyachaudhary2468
    @soumyachaudhary2468 ปีที่แล้ว

    No need to make the third else condition that large:
    void addNum(int num) {
    int lsize = maxheap.size();
    int rsize = minheap.size();
    if(lsize==0){
    maxheap.push(num);
    }else if(lsize == rsize){
    int rtop = minheap.top();
    if(num > rtop){
    minheap.pop();
    maxheap.push(rtop);
    minheap.push(num);
    }else{
    maxheap.push(num);
    }
    }else{
    int ltop = maxheap.top();
    if(num > ltop){
    minheap.push(num);
    }else{
    maxheap.pop();
    minheap.push(ltop);
    maxheap.push(num);
    }

    }

    }

  • @rishisahoo2772
    @rishisahoo2772 ปีที่แล้ว

    you can follow this too: class MedianFinder {
    public:
    priority_queuemaxHeap;
    priority_queueminHeap;
    MedianFinder() { }

    void addNum(int num) {
    maxHeap.push(num);
    minHeap.push(maxHeap.top());
    maxHeap.pop();
    if(minHeap.size()>maxHeap.size()){
    maxHeap.push(minHeap.top());
    minHeap.pop();
    }
    }

    double findMedian() {
    if(maxHeap.size()>minHeap.size()) return double(maxHeap.top());
    else return (double(maxHeap.top())+ double(minHeap.top()))/2;
    }
    };

  • @pritishpattnaik4674
    @pritishpattnaik4674 3 ปีที่แล้ว

    great explanation

  • @hemantvardani1436
    @hemantvardani1436 2 ปีที่แล้ว +1

    Thanks

  • @ai7560
    @ai7560 3 ปีที่แล้ว +1

    Thanks