Median of stream of running integers | Heaps, Priority Queues Application | Explanation from Basics

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ม.ค. 2025

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

  • @amanaziz2785
    @amanaziz2785 4 ปีที่แล้ว +64

    Thank you for giving such a simple explanation. I am a pre final year engineering student and I watched this video a few weeks earlier. The same question was asked to me in my technical interview yesterday when I sat for internship and the interviewer was really impressed with the way I explained it. I secured an internship there and this question has a big role in it. Thank you so much. Before watching this video I had no idea how to do this but your explanation made it really simple. I have watched your other videos as well. The way you explain is really amazing. Thanks again :)

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

      This made my day!
      Thank you so Aman 🙂 Congratulations and all the best for your internship 😇😇

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

      @@KeertiPurswani Thanks :)

    • @sameer1571
      @sameer1571 4 ปีที่แล้ว +6

      I do not know how to say it but 1 week back I saw this video i just understood it and even did not write the code yesterday this question was asked in Walmart interview and i just explained it and got selected too..thanks to you di :) :)

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

      Congratulations Sameer. So happy for you!!❤️
      And thank you for sharing with me 😇

  • @AbhiNandan-ct9or
    @AbhiNandan-ct9or 3 ปีที่แล้ว +6

    This is by far the most intuitive and understandable solution on internet. The typical solution provided by leetcode is confusing and difficult to follow. Thanks for this video and keep up the good work!

  • @nagsenwaghmare5115
    @nagsenwaghmare5115 4 ปีที่แล้ว +12

    I liked the way you are increasing confidence of those viewers who are unable to understand by using punch lines such as : "if it is still not clear, it is ok we will go through example and see!" That's something cool mam Thanks for such Amazing video 😀😀

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

      I have struggled with these questions myself in the past. I know how overwhelming it can be. So I tru my best to keep it simple and keep viewers motivated ❤️

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

      @@KeertiPurswani You're too kind :') thank you so much for the SUPER smooth explanation

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

    For people like me, the core concept here is to use max heap to store the left half (of sorted elements) and min-heap to store the right part (of sorted elements). So as to get the maximum of left part and minimum of right part (because we only need the middle section if we arrange elements in sorted order).
    8:14 onwards, Keerti explains why we need to do that...
    Precise content, kudos to you!

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

    Loved it! Never seen someone explaining a hard level question so calmly and intuitively ! Thank you keerti 👏👏👏

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

    Itna preshan hone ke bad abh is video ko dekhne ke bad code smjh aya !
    Thank you For such a clear , calm and Intuitive Solution.

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

    Preparing for my interviews. The best thing about Keerti is she keeps encouraging that is even if you haven't understood then she is going to explain it even further. That helps me keep going and not close the video.
    Also, the explanation is very well done. Thanks Keerti
    💛💛💛

  • @Kartik-o1
    @Kartik-o1 2 ปีที่แล้ว

    Best and fluid explanation. Even in the first 5 minutes got the intuition and the next explanations are cherry on top. Thanks again.

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

    Your explanation skills are top notch. A calm river slowly flowing out to the sea . Same rythm smooth flow

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

    The best explanation ever. I was stuck in this question from past two days and then I watched ur video..omg I solved it with in an half an hour 🔥
    Though some bugs were there in my code before 😂😂😂

  • @vishnusingh4118
    @vishnusingh4118 4 ปีที่แล้ว +5

    Lovely explanation Keerti. Great job! One suggestion though, you seem to be not caring about the code implementation (from what you said and from your past videos). That is understandable as when we reach an advanced level, an algo is all we need and a proficient coder will have no problem converting the algo to code even while they are drunk. But isn't your channel for beginners ? I can tell you code is everything for a beginner! What does one do with the algo if they can't implement it ? It would have taken you a few mins and people would have got clarity to how to convert all this "English" into actual working code. "priority_queue mypq" - Oh! that's how u implement it in code". Means the world! I guess it's a lot more hardwork ? But to be honest, most books, videos do exactly what you're doing. They just explain the algo. But if there's a beginner listening to this (which I assume is your target audience, since you break down the problem so nicely into understandable chunks) therefore isn't it obvious that they will not know how to implement a pqueue in C++ (or Java etc) ? When I was learning all this, the biggest pain point was that everyone would talk about the algo, but no one would show the code! If you show, the actual code - half the concept is clear from there itself! It is ek teer do shikaar - you can do a dry run of the algo through the code as well, and the viewer gets to see the implementation. Otherwise, I feel it is more like just hand waving in the air (again - only for beginners!). I understand looking up code is just a Google search away, but then so is looking up the algo, isn't it ? :p.
    Please don't take it as criticism. It is only a suggestion. I think you're doing a fantastic job! You don't have to listen to me. Thanks for another lovely vid again! :)

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว +6

      Hi Vishnu, thank you so much for your comment. Such discussions are always welcome :)
      As you said, the channel is for beginners and that is the reason I don't want to make any beginner feel that they should be restricted to one particular language. Suppose I told the syntax in C++ or java, and someone who codes in python asks why I didn't cover his language - what will I tell him/her?
      Infact, in most of the companies, language doesn't matter, as long as you are able to write the algorithm which I did.
      Also, yes both algorithm and syntax are a Google away but syntax is about copy pasting (or remembering) but algorithm is about understanding the concept behind it.
      I want to encourage people to go behind the logic. The syntax is something that you can just copy paste.
      And about Dry run, I did it for the code I wrote yeah? The only thing I didn't write was how to declare the priority queue 😅
      I hope I make sense?

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

      @@KeertiPurswani i like your way of explanation. By dividing into chunks.
      I agree to the point (not giving code).
      You make me clear in algorithm. Converting into code is jst syntax thing.
      P.S : we have to do some hardwork to learn something.
      P.P.S : if possible create telegram channel. So we can discuss there.
      ThankYou

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

      @@KeertiPurswani I loved the way you handled the comment. Very camly!!

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

    Calm and detailed explanation. Appreciate you for a quick recap every few minutes. Keep up the good work.

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

      Glad you liked it! Thank you 😇

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

    You explain so calmly and nicely. Absolutely what I required. Thank u so much for making these videos. Even if some other videos are shorter, I would still prefer yours.

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

    Best explanation for this problem by dry running it, in the whole TH-cam.

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

    Loving the success stories in the comments. You are definitely a great teacher Di !

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

      Thank you Aakash, means so much ❤️
      Success stories are my fav too😇😇

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

    Your explanation was awesome.
    This is the C++ implementation.
    (I have used integer priority queue, but we can also use double.)
    #include
    using namespace std;
    int main() {
    //code
    int n;
    cin>>n;
    priority_queue maxheap; //Lower Half
    priority_queue minheap; //Upper Half
    int a;
    while(n--)
    {
    cin>>a;
    if(maxheap.empty() || maxheap.top()>a)
    {
    maxheap.push(a);
    }
    else minheap.push(a);

    if(maxheap.size()>minheap.size()+1)
    {
    minheap.push(maxheap.top());
    maxheap.pop();
    }
    else if(minheap.size()>maxheap.size()+1)
    {
    maxheap.push(minheap.top());
    minheap.pop();
    }
    if(minheap.size()==maxheap.size()) cout

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

      Great job @Akash. Thanks for sharing 😊😊

    • @AK-yb4ml
      @AK-yb4ml 4 ปีที่แล้ว

      Is " a " here is running number or stream here.

    • @AK-yb4ml
      @AK-yb4ml 4 ปีที่แล้ว

      If input is vector of Integers then would it give correct answer.

    • @sigma9089
      @sigma9089 4 ปีที่แล้ว

      @@AK-yb4ml Yes, if input is vector you can modify the program accordingly to take input as a stream.
      If input is a vector you can run a loop and instead of using variable a you will write v[i] if v is your vector.

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

    Great explanation Keerti, most of your content is very clear, concise and can be understood even by newbie's. You are the true inspiration, motivator and very good mentor to most techie's

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

    Kya hi explain kiya hai, maja aa gya. Sab samajh aa gya. 🧡 from remote.

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

    One of the most fluid explanations of a problem that I have come across!

  • @danishirfan9317
    @danishirfan9317 4 ปีที่แล้ว

    I was practicing this question on geeksforgeeks but couldn't understand the explanation given there. So after searching for a video on youtube, I found yours and completely understood the logic.
    Thank you for making this video! I've subscribed to your channel 😊

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

      Thank you so much Danish. Means a lot 😇

    • @danishirfan9317
      @danishirfan9317 4 ปีที่แล้ว

      @@KeertiPurswani You're welcome 😊

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

      @@KeertiPurswani same happened with me...Thanks, it really helped me a lot :)

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

    thanks for sharing such gem on youtube I must say this you made this hard level problem like an easy solution
    NOTE : in c++ you've max priority queue by default so to make a min priority queue just multiply -1 with the element while pushing or popping from a queue
    once again thank you so much dii ;)

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

    Keerti please keep going and you will be one of the best teachers in this field very soon.

  • @sadhiyashiraj6990
    @sadhiyashiraj6990 4 ปีที่แล้ว +6

    This is great Keerti, proud of your hardwork!! 😍😍

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

      Thank you so much ❤️

    • @audioplatform6299
      @audioplatform6299 4 ปีที่แล้ว

      why you proud of her work? just curious

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      She is my friend 😇

    • @audioplatform6299
      @audioplatform6299 4 ปีที่แล้ว

      @@KeertiPurswani got it!.. Very good channel for learning.. good luck!!

  • @vulturebeast
    @vulturebeast 4 ปีที่แล้ว +11

    This question was Best explained here 👍🏼

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

    Mam hats off to your explanation only listening to the explanation made the code crystal clear thank you for this amazing content

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

    Thank you for the great explanation.
    Java code with stream of integers generation and finding the median is given below:
    import java.util.PriorityQueue;
    import java.util.Random;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    public class RunningMedian {
    public static void main(String args[]) {
    RunningMedianUtility runningMedianUtility = new RunningMedianUtility();
    // stream of integers
    IntStream.generate(() -> new Random().nextInt(50)).limit(new Random().nextInt(50)).forEach(
    runningMedianUtility::add
    );
    //Display values of min and max heap
    runningMedianUtility.display();
    System.out.println(runningMedianUtility.getMedian());
    }
    }
    class RunningMedianUtility {
    private final PriorityQueue maxHeap;
    private final PriorityQueue minHeap;
    public RunningMedianUtility() {
    this.maxHeap = new PriorityQueue((o1, o2) -> {
    return -1 * o1.compareTo(o2);
    });
    this.minHeap = new PriorityQueue();
    }
    public void add(Integer value) {
    if (maxHeap.isEmpty() || maxHeap.peek() > value) {
    maxHeap.add(value);
    } else {
    minHeap.add(value);
    }
    // Re-balancing
    if (maxHeap.size() > minHeap.size() + 1) {
    minHeap.add(maxHeap.poll());
    } else if (minHeap.size() > maxHeap.size() + 1) {
    maxHeap.add(minHeap.poll());
    }
    }
    public double getMedian() {
    if (maxHeap.size() > minHeap.size()) {
    return maxHeap.peek();
    } else if (minHeap.size() > maxHeap.size()) {
    return minHeap.peek();
    } else {
    return (minHeap.peek() + maxHeap.peek()) / 2.0;
    }
    }
    public void display() {
    System.out.println(maxHeap.stream().map(o -> String.valueOf(o)).collect(Collectors.joining(",")));
    System.out.println(minHeap.stream().map(o -> String.valueOf(o)).collect(Collectors.joining(",")));
    }
    }

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

    👍great explanation! She and Jenny are the best indian women youtubers who are expert in DSA.✌

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

    Nice explanation Keerti. I understood your approach and solved this question on leetcode watching first 10 minutes of video

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

    Excellent, excellent, excellent explanation! Thank you for sharing this! this really helps with a hackerrank problem set

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

    Crystal clear explanation for a supposedly hard question!

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

    Keerti your lecture is so beautiful. Soft and clear explanation. You combined two of the best things of this world, beauty and brilliance. So i rate you as b++.

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

    This is a very clear explanation. Good job Keerti! So proud!!!

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

    Wonderfully explained, not gonna forget this for the rest of my life

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

    Thank you Keerti for the amazing explanation. this is now imprinted in my mind.

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

    The best explaination Keerti , it makes the question difficult to forget !

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

    This is the best and the smallest possible code available 🔥🔥 and yeh best explantion thank u Ma'am

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

    Thank You very much I watched several videos but couldn't understand but after watching this video I was able to fully understand it.

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

    Great explanation, explained in very simple terms, can be understood for the very beginners in the programming.

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

    ohhh my god!! , what a clear understanding I got after watching this video, thanku so much mam. loved it

  • @shivambansal3560
    @shivambansal3560 4 ปีที่แล้ว +2

    The Best Explanation Ever !!!

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

    what an explanation 😲 Fantastic !!

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

    This is the best explanation of this question i've got so far..thanks!

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

    such shortcode for this hard level marked question was just unimaginable! looking on for more of your videos! thank you, saviour! :p

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

      Haha yeah, it’s all about the concept!
      Thank you so much for all the love and support❤️

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

    GOD LEVEL EXPLANATION

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

    Hard problem + best explanations = problem become a cakewalk.

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

    Very nice explanation. Could not have cracked this approach in live interview before.

  • @rishavsinghmehta52
    @rishavsinghmehta52 4 ปีที่แล้ว +2

    Great teaching sense and awesome explanation.... 🔥🔥🔥🔥

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

    No doubt this is a difficult question but, the way you explained it made this question really easy. Thank you Ma'am .

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

      Thank you so much Tanmay 😇🙏🏻

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

    Very Simple step by step explanation

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

    Just one word - "Excellent"!

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

    Very smoothly explained. Thanks Ma'am.

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

    I appreciate your patience and calmness! Really look up to you Keerti di :)

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

      Thank you so much Ravisha. Means a lot🙈❤️

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

      @@KeertiPurswani ♥️

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

    hey good that you mentioned insertion sort. We never think of quadratic sorts while giving out a brute force approach. I think its a good way to start at an interview.

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

    Really nice and simple explanation over youtube!!!...

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

      Thanks Aman 😇 Hope you like rest of the videos as well 😇

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

      @@KeertiPurswani Yes...😍

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

    Thanks a ton didi.Providing such amazing content for free

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

      Blessed to be able to do that 😇😇

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

    Wonderful explanation for Heaps. Hard to find such simple explanation anywhere else.

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

      Thank you so much Naresh. Means a lot. Hope you like rest of the videos as well 😇😇

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

      @@KeertiPurswani yes I have started watching of them. You have put a lot of effort in making quality content. Great work and thanks for your contributions. Its really helpful.

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

      Thank you 🥺🥺😇😇

  • @capy_can_code
    @capy_can_code 4 ปีที่แล้ว

    Awesome, the logic can be further simplified, if you push to maxheap and then poll the top value from maxheap and push it to minheap. Maintain the difference between size to max one.

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      That's exactly what I am doing 😅 what is the simplification suggested?😅

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

    This question is quite simple to understand and to implement. If you really want to show your algorithms skills you should do a video on median of two sorted arrays in log(n) time. I'm quite sure you wouldn't be able to.

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

      There’s already a video on that, and don’t be so sure because everyone who watched the video can do it for sure ✌️

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

    Thanks you for giving such a simple explanation

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

    you explained the question soo well that now it feels like an easy problem 😄

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

    Your explanations are easy to understand. Keep doing great work.

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

      Thank you Ramesh, means so much❤️

  • @shubhamkumar-gw4vb
    @shubhamkumar-gw4vb 3 ปีที่แล้ว

    well done keerti, it was really explained well....kudos!!!!!!

  • @songs-pu9bq
    @songs-pu9bq 3 ปีที่แล้ว

    Loved the way you made it so simple

  • @Anonymous-pj2mv
    @Anonymous-pj2mv 4 ปีที่แล้ว

    Unbeatable explanation .
    Really great work di .

  • @devprakash5320
    @devprakash5320 4 ปีที่แล้ว

    she explained it beautifully . great video .

  • @akhilsharma1778
    @akhilsharma1778 4 ปีที่แล้ว

    I could not figure out when to insert in min-heap and when to insert in max-heap(which is the most important part). Thanks for the explanation. OP :P

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

      Please watch the video once more? I am sure you will understand!

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

    Amazing explanation. God's work!

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

    Best explanation ever!!!

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

    Thank you very much

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

    Thank you for the awesome content always.I finally understood this hard problem.Best explanation ever

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

    Thanks Keerti. Keep your good work !

  • @sackshamsharma2829
    @sackshamsharma2829 4 ปีที่แล้ว

    this problem doesn't look hard enough after your explanation, loved the explanation please make more videos.

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      Thank you so much. Many videos coming up 😇😇

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

    This was a very easy explanation I have ever seen. Thank you

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

      Thanks Patel, means a lot 😇😇

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

      Haha First name is Raj 😅

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

      Oh sorry😅😅🙈🙈

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

      No worries 😅

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

    Glad I found your channel !💯😍

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

    I just Want to know what's the difference if we use a single set/heap . What will be the reduce in time complexity by using 2 heap

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

    Time Complexity is 3.logn(At worst case we perform 2 push and 1 pop) and not nlogn please correct me if I am wrong, We should not consider n as the size of the array as it is a running stream.

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

    Best explanation ever!

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

    U made it soo easy to understand 🔥🔥🙏🏻🙏🏻🎉🎉

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

      Yaaay, thank you ❤️❤️ hope you like rest of the videos as well 😇😇

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

    such a clear explanation ! Thank you so much Di

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

      Thanks Prerna. So glad you like it!
      Please do share the channel with your friends and help it get better reach 😇🙏

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

    Thanks for the explanation The solution is overall good but i personally could not wrap my head about how i will explain this approach to the interviewer as when you explained how we are inserting the number in our heaps as that is difficult to come up with on our own instead i think we should proceed by taking different cases and add number by comparing them to the current median and maintaining the size difference.

  • @Deepak-gj4ni
    @Deepak-gj4ni 3 ปีที่แล้ว

    Please make more videos on such questions. We need more teachers like you..

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

    Good work! Just for the sake of completeness, I want to add some additional insights, please feel free to correct me if I got anything wrong.
    The main advantage of this approach is that it improves the time complexity for retrieving the median for a read heavy system in the presence of stream data. The time to retrieve max/min from priority queue is constant. Thus, the time to calculate the median from a stream of data is reduced to constant time. Pause to think about it, this is a big deal!
    Hadn't we use this arrangement for storing the numbers, the alternatives are really, saving the numbers in a sorted fashion in some form of set or vector. Vector is not the right choice since the input is large and we might keep resizing the vector to fit elements. In the worst case the vector keeps growing and bottlenecks the system, alternately we could chunk the array and put it in memory that has its own drawback (finding median in K sorted arrays :()
    The time complexity of retrieving median from a sorted structure is log N (if the structure has say N elements) and in a read heavy system, if I keep making calls to compute median a large number of time (say K) the overall complexity to calculate the median increases to KlogN or NlogN!
    The approach that Keerti pointed out reduces it constant time!
    If you have made this far, thankyou! Brownie insight...we can get the median by not really saving all the elements. We can think of equal fractions of elements we can eleminate from the heap and still get median in constant time.
    Illustration (for a sorted array size > 4)
    a1 , a2 , a3 .... ak, ak+1...aN-3, aN-2, aN-1
    Can you convince yourself that the median won't change if I knock of (a1 and aN-1) and inductively if I knock off (a1, a2, aN-2 and aN-1)??
    Food for thought? What else will you gain by this approach for a system that only need median and not the data really?
    Once again! Good job Keerti, great content, just wanted to add some more insights for completeness.

  • @nishantmiddha9161
    @nishantmiddha9161 4 ปีที่แล้ว

    Thanks Keerti. It was well explained. Need your help with one question. I solved it in O(n) time and space complexity but when I looked out for the solution it was solved in O(nlogn) time and O(n) space complexity.
    Just need to verify whether my approach is correct or not.
    "Write a function to count number of smaller elements on right of each element in an array. Given an unsorted array arr[] of distinct integers, construct another array countSmaller[] such that countSmaller[i] contains count of smaller elements on right side of each element arr[i] in array."

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      Hi Nishant
      Please share the approach you followed. There can be multiple solutions to this question but I doubt if it is possible is O(n) time

    • @nishantmiddha9161
      @nishantmiddha9161 4 ปีที่แล้ว

      @@KeertiPurswani Below mentioned is my approach. Please note I am assuming input array contain distinct integers.
      1. Consider "nums" as your input array and "result" as your final output array.
      2. Take another array "nextSmaller" with size the same as of nums.
      3. nextSmaller[i] stores index of element which is present on right of nums[i] and is just smaller than nums[i].
      If no smaller element exists to right of nums[i] then nextSmaller[i] = -1.
      4. result[n-1] = 0 where n is size of input array.
      5. for j=n-2 to 0
      result[j] = nextSmaller[i] != -1 ? result[nextSmaller[i]]+1 : 0;

    • @nishantmiddha9161
      @nishantmiddha9161 4 ปีที่แล้ว

      @@KeertiPurswani You are right. It can't be solved in O(n). Now I know where I got wrong. Thanks :)

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      Awesome Nishant. This is how we learn the most, learning from our own mistakes 😇😊

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

    The explanation and dry run was awesome. Kudos to your great efforts!

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

    Your explanation is soo good, I am able to write code by myself, But I want to know why we dont simply use vector O(1) and find mid element with help of Quick Serach O( Nlog(mid) ).

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

      The numbers come in a potentially endless stream. You would have to do quick sort every time you need to find the median. The insert sort would keep the list ordered, making it cheap to find the median at any time, but it's still slower than this maxheap/minheap solution.

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

      @@Rothron Thank You, I implemented this and find , it taking more time by using Quick Serach every time

  • @techenthusiast3966
    @techenthusiast3966 4 ปีที่แล้ว

    Best explanation of the question and solution.

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

    Great explaination. I am watching your video for the first and really liked it. Keep doing the same.

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

    Best Explanation 💖

  • @SumitKumar-fn3gj
    @SumitKumar-fn3gj 3 ปีที่แล้ว

    thank you for such wonderful explanation

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

    Beautiful Explanation !!

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

    Mam You are doing great work . please consider more hard problems from Leetcode .

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

    beautiful explanation!
    Loved it.....

  • @neelakshsharma4936
    @neelakshsharma4936 4 ปีที่แล้ว

    Amazing explanation of the solution. Please make more and more videos like this. Thank you.

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

      Thank you. Many videos coming up 😊

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

    Very good explanation! Thanks

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

    Really great explanation. Loved it

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg 4 ปีที่แล้ว

    Woah! Great explanation Mam...

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      Thanks Rajat! 😇 please do share the channel with your friends for better reach 😊

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

    Thank you Di..
    You are amazing!

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

    Great explanation..
    Love you from bottom of my heart.. 😍
    If possible please provide the links to he code.

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      Thank you 😊😊
      Which part of code do you need help with?

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

      @@KeertiPurswani
      Your explanation was very clear.
      I implement everything in code. And got AC.
      But i feel if you provide link to code. It is awesome.
      ThankYou..

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว

      @@nagavijaykumarprathi8531 I try to explain in the way that one can write the code, like you did 😊 if I write code in a language, it will restrict viewers mind to that language and I really don't want to do that!

    • @nagavijaykumarprathi8531
      @nagavijaykumarprathi8531 4 ปีที่แล้ว

      @@KeertiPurswani Really you explained very well.
      ThankYou for your efforts. 😍

  • @cakec9
    @cakec9 4 ปีที่แล้ว

    Excellent explanation in the Visualization part.

  • @mohanbhati9595
    @mohanbhati9595 4 ปีที่แล้ว +2

    Best one explanation.