Merge K sorted lists | Leetcode #23

Поделиться
HTML-код
  • Опубликовано: 24 окт 2024

Комментарии • 94

  • @eduardozevallos1509
    @eduardozevallos1509 3 года назад +45

    if there isn't an explanation by tech dose for a leetcode problem i feel lost. You are the best!

  • @ajaywadhwa3398
    @ajaywadhwa3398 3 года назад +42

    You are a Pro Bro . Every Time I stuck on Problem I just type the problem and in suffix Tech Dose. and Boom.... Bagwan Prakat !!!!
    Really Appreciate your efforts towards community .

  • @KeshariPiyush24
    @KeshariPiyush24 3 года назад +3

    Of all the videos in this playlist this one is little bit tricky.
    It would be such a blessing to have an elder brother like you.
    Thank you very much for all the efforts you are making.

    • @techdose4u
      @techdose4u  3 года назад

      👍🏼

    • @KeshariPiyush24
      @KeshariPiyush24 3 года назад

      @@techdose4u please make a video on bit manipulation leetcode 1486 question It is an easy problem but I am not able to solve it in O(1) time complexity...read so many articles on it but still can't figure out how it is working

  • @atishaysrivastava7772
    @atishaysrivastava7772 3 года назад +9

    Now I got one more question covered. Thanks!! Please make video on "Find median in a stream".

  • @kumarc4853
    @kumarc4853 3 года назад +6

    Sir, Thank you for the awesome videos. Please also consider making videos on various Patterns of Interview problems, examples and how to build strong intuition to spot these patterns.
    example: Two pointers, Monotonic queue, heap, two heaps, when to use union-find vs graphs, intervals/scheduling etc

  • @patilpradyumna2994
    @patilpradyumna2994 2 года назад

    Your explanation is really good.. the way you giving the example and explained it ,it’s really fab. man.

  • @glenfernandes253
    @glenfernandes253 3 года назад +4

    great content! Please continue doing this. Planning on completing all your videos.

  • @gandhijainamgunvantkumar6783
    @gandhijainamgunvantkumar6783 2 года назад +5

    Simple code for the last approach
    time complexity: O(nlog2k)
    space complexity: O(k) (for using priority queue)
    //compare function for priority queue
    class Compare
    {
    public:
    bool operator() (const ListNode* n1, const ListNode* n2)
    {
    return (n1->val) > (n2->val);
    }
    };
    class Solution {
    public:
    ListNode* mergeKLists(vector& lists) {

    if(lists.size() == 0) return NULL; //MIMP edge case for interview
    priority_queue pq;

    //pushing first nodes of all the lists into the priority queue
    for(int i=0; inext != NULL){
    pq.push(node->next);
    }
    }

    return head;
    }
    };

  • @agarwalshubham23
    @agarwalshubham23 3 года назад +9

    Awesome explanation.
    I think the time complexity of the last 2 approaches will be O(N * k * log(k)). Correct me if I'm wrong.
    @TECHDOSE

    • @spetsnaz_2
      @spetsnaz_2 3 года назад +1

      No...It would be NlogK only....Since we are traversing each list at the same time, so O(N+k) for traversal and each traversal will take O(logK) time to push/pop from Min-Heap

  • @sakshisahu3983
    @sakshisahu3983 2 года назад

    Randomly searched for the solution, and here I am on this channel...very nice approach and easily explained. Thank you so much for all the leetcode problems you explained. Just subscribed.

  • @ankushsinha4683
    @ankushsinha4683 3 года назад +3

    Sir please make playlist on string hard problem on leetcode. Thankyou so much sir for your videos. 💥💥🙏🙏

    • @techdose4u
      @techdose4u  3 года назад +2

      Please recommend this when I start String algo playlist.

  • @iamparitosh
    @iamparitosh 3 года назад +2

    Best Channel for average students

  • @karanprabhakar72
    @karanprabhakar72 2 года назад +1

    Time complexity is wrong for last 2 . It should be NKlog(k) . Please correct it

  • @deepikagoyal07
    @deepikagoyal07 Год назад

    Here build Heap,percolate up is being called implicitly by the c++ priority_queue?

  • @rathan235
    @rathan235 Год назад

    Great job

  • @andrejoljaca3577
    @andrejoljaca3577 3 года назад +2

    Amazing! Keep doing these please

    • @techdose4u
      @techdose4u  3 года назад

      Sure :)

    • @andrejoljaca3577
      @andrejoljaca3577 3 года назад +1

      @@techdose4u only thing is if you could do it in java too and explain as you write the code from the beginning. Just my opinion though. It’s hard to just copy lots of code at once.

  • @Spham99
    @Spham99 2 года назад

    What is the point of creating 'ptr' when you could use 'lists'? Why initialize ptr? Is it because that's a dummy ListNode since we need to use 'lists' later on? Why do we need to create a pointer for that when we could just iterate through the 'lists' and go to the next ListNode?

  • @dvanscollection1413
    @dvanscollection1413 2 года назад

    great explanation.

  • @jiosim1377
    @jiosim1377 3 года назад +1

    The starting code of heap approach is confusing can someone explaine?

  • @iSumitYadav
    @iSumitYadav 3 года назад +3

    Bro please solve Alien Dictionary. Thanks 👍🏻

  • @nareshh74
    @nareshh74 Год назад

    First approach takes o(n) and not o(nk) i think. Can u double check?

  • @Jaffar87
    @Jaffar87 2 года назад

    sir do all leetcode problems as well codechef

  • @sushantkumarrout2198
    @sushantkumarrout2198 Год назад

    Thank you sir

  • @neetisaharan1340
    @neetisaharan1340 3 года назад +1

    very well explained , thanks a lot

  • @ashutoshsanodia5690
    @ashutoshsanodia5690 3 года назад +1

    Excellent choice of question 😉

  • @andriidanylov9453
    @andriidanylov9453 8 месяцев назад

    👍 5methods, wow, thank You

  • @JaiPrakash-ku7it
    @JaiPrakash-ku7it 3 года назад +2

    Hello sir,
    Please explain Tower of Honoi if possible. Because I have tried to get it since last one year but never satisfied with those videos.

    • @techdose4u
      @techdose4u  3 года назад +1

      Sure. I will include those when I do recursion.

    • @JaiPrakash-ku7it
      @JaiPrakash-ku7it 3 года назад

      @@techdose4u thanks!

  • @mayurgupta136
    @mayurgupta136 3 года назад +1

    good work bro, keep it up.

  • @rohandevaki4349
    @rohandevaki4349 2 года назад

    sir did you make any video for merge k sorted arrays?

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 2 года назад

    Thanks, Which are you using for the annotations and making the drawings.

  • @the_sneaky_worms
    @the_sneaky_worms 2 года назад

    thanks for sharing

  • @TekkerReviews
    @TekkerReviews 3 года назад +2

    Very helpful video, Thank you :)

  • @sudharshankrishnakumar9321
    @sudharshankrishnakumar9321 3 года назад +2

    Thanks!

  • @ganeshkamath89
    @ganeshkamath89 2 года назад

    Why can't we push all nodes from all lists to the heap and then pop all elements till heap becomes empty? That will even sort K unsorted Linked lists.

  • @anmolkohli6299
    @anmolkohli6299 3 года назад

    Great video sir . Just wanted some clarification in divide and conquer approach that isn't the heap method better than it as in divide and conquer we are also using stack memory ?

  • @revanth6476
    @revanth6476 2 года назад +1

    thank u sir..!!

  • @nathanthurston2332
    @nathanthurston2332 2 года назад

    Why not to push all Nodes into heap with one iteration which is O(N*logk), then simply pop O(N*logk) and build the final linked list?

  • @dipanjanjayswal554
    @dipanjanjayswal554 3 года назад +1

    which software do you use for teaching bhaiya?

  • @MyLifeMyWorld08
    @MyLifeMyWorld08 3 года назад +1

    what about space complexity ti create heap of size k?

  • @sayantaniguha8519
    @sayantaniguha8519 2 года назад

    Why am I not getting TLE when I submit O(nk) solution ?

    • @abhi36292
      @abhi36292 2 года назад

      add a
      while(true){ }
      snippet in the code

  • @sauravchaudhary8653
    @sauravchaudhary8653 3 года назад

    Heap approach using array of pointers
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    struct node{
    ListNode* curr;
    int idx;
    node(ListNode* a,int x){
    curr=a;
    idx=x;
    }
    };
    struct compare{
    bool operator()(node a,node b){
    return a.curr->val > b.curr->val;
    }
    };
    ListNode* Solution::mergeKLists(vector &A) {
    priority_queue minh;
    int k =A.size();
    vector ptr(k);
    for(int i=0;inext;
    // crucial
    if(ptr[index]!=NULL) minh.push(node(ptr[index],index));
    while(!minh.empty()){
    ListNode* temp=minh.top().curr;
    index=minh.top().idx;
    minh.pop();
    tail->next=temp;
    tail=tail->next;
    ptr[index]=ptr[index]->next;
    if(ptr[index]!=NULL) minh.push(node(ptr[index],index));
    }
    return head;
    }

  • @joyalmeida6565
    @joyalmeida6565 3 года назад

    in heap approach why don't we add all elements in the min heap first and then pop them 1 by 1

    • @karanveersingh5535
      @karanveersingh5535 3 года назад

      Coz its complexity would then be o(nlogn) same as that of merge sort.

  • @saisrikantta7648
    @saisrikantta7648 2 года назад

    I don't think we need to maintain an index array.

  • @prithvigupta8215
    @prithvigupta8215 Год назад +1

    17:00

  • @saniajawaid7432
    @saniajawaid7432 3 года назад +1

    Heap Algorithm won't work for such examples:
    K = 4
    L1 = [1 2 3 4]
    L2 = [0 5 10 15]
    L3 = [2 4 8 10]
    L4 = [3 9 27 81]
    Expected Output:
    0 1 2 2 3 3 4 4 5 8 9 10 10 15 27 81
    Output according to this solution:
    0 1 2 2 3 3 4 5 4 8 9 10 10 15 27 81
    Reason:
    Heap of size K cannot accommodate all smallest numbers from all K arrays at once.

    • @mysymbol
      @mysymbol 3 года назад

      Heap has to retain max K element at once, not more than that. Heap size will be less than K only when you have exhausted one or more linked list.

  • @mazenyasser8299
    @mazenyasser8299 2 года назад

    The best

  • @jatinbhatoya8420
    @jatinbhatoya8420 3 года назад

    hey guyz what if we start td old videos in background on mute so that he will get more watch time on his channel and ultimately earn more.

  • @aravindswamy3874
    @aravindswamy3874 3 года назад +1

    Bro please solve "can place flowers" Leetcode 605

  • @katchen2626
    @katchen2626 Год назад +1

    bless u

  • @CodeSuccessChronicle
    @CodeSuccessChronicle 3 года назад +3

    waiting 😇

  • @hareeshkumar922
    @hareeshkumar922 3 года назад

    Can anyone explain compare function in priority_queue

  • @TomerBenDavid
    @TomerBenDavid 3 года назад

    Samson q2u?

  • @rayhanjhony75
    @rayhanjhony75 Год назад

    // Without using list index in priority queue
    class Solution {
    struct node {
    ListNode *curr; //Current node
    node(ListNode *a)
    {
    curr = a;

    }
    };
    struct compare {
    bool operator()(node a, node b)
    {
    return a.curr->val >b.curr->val;
    }
    };
    public:
    ListNode* mergeKLists(vector& lists) {
    int k = lists.size();
    if(k==0)
    return NULL;
    ListNode *head,*tail;
    head=tail=NULL;
    priority_queue heap;
    //Assign all the current ptrs and BUILD_HEAP
    for(int i=0;inext) //Push newly pointed node if not NULL
    heap.push(node(tail->next));

    //Make list with rest of the nodes
    while(!heap.empty())
    {
    ListNode *temp=heap.top().curr; //Pop root node
    heap.pop();

    tail->next=temp; //Add newnode to list
    tail=tail->next;
    if(temp->next) //Push newly pointed node if not NULL
    heap.push(node(temp->next));
    }
    return head;
    }
    };

  • @ahmadbodayr7203
    @ahmadbodayr7203 Год назад

    read about islam man❤

  • @yihongliu7326
    @yihongliu7326 2 года назад +1

    Thanks!