Minimum Number of Operations to Sort a Binary Tree by Level | Leetcode 2471 | codestorywithMIK

Поділитися
Вставка
  • Опубліковано 26 гру 2024

КОМЕНТАРІ • 66

  • @gamerboy188
    @gamerboy188 4 дні тому +32

    Hey bro, I hope you had an incredible trip! I just want to take a moment to thank you for everything you're doing. I currently work at TCS with a very low package, but your support has really motivated and inspired me. I'm feeling super pumped, and I hope to transition to a better company soon. Thanks again for everything, Mik. I truly pray that all your dreams come true.

    • @gui-codes
      @gui-codes 4 дні тому +4

      You got this bro. I am also in a similar path and got a job in a service based company and now preparing for product based company. Let's do this.

    • @gamerboy188
      @gamerboy188 4 дні тому +2

      @@gui-codes we got this bro , let's gooooo

    • @arnabsarkar5245
      @arnabsarkar5245 4 дні тому +1

      All the best buddy.

    • @aws_handles
      @aws_handles 4 дні тому +1

      Let’s do this 🔥🔥🔥

  • @dibbodas4116
    @dibbodas4116 4 дні тому +12

    finally after few days mik sir is back✅✅

  • @gui-codes
    @gui-codes 4 дні тому +7

    The LEGEND is back 🔥
    Waited eagerly to see your videos.

  • @Believe_yourself-eo1ho
    @Believe_yourself-eo1ho 3 дні тому +5

    I am done BRUTE FORCE
    BFS + Selection Sort Thank you you are back ❤❤❤❤
    Please continue DP CONCEPT WALA PLAYLIST 🙏🙏🙏

  • @amanpaliwalvlogs6860
    @amanpaliwalvlogs6860 3 дні тому +3

    Radhe Radhe ❤❤

  • @ravirathore6717
    @ravirathore6717 4 дні тому +5

    I solved it today on my own, thanks for boosting my confidence.

  • @ansh6552
    @ansh6552 4 дні тому +8

    Finally MIK sir is back ❤️
    Sir ho sake to kal wala potd karado copy paste kiya tha kal wala 😅

  • @parulawasthi7353
    @parulawasthi7353 3 дні тому +1

    Bhaiya i have been following you for last two months and i could see growth in my problem solving ability. Thankyou so much for providing us with the intuitiveness to solve the questions.
    If possible then please cover the leetcode and codechef contests as well.

  • @robot3.077
    @robot3.077 4 дні тому +7

    Sir kal ke potd ki ek video solution bana dena.
    wo infosys ke interview me poochaa gaya tha

  • @shraban8508
    @shraban8508 4 дні тому +3

    Finally you are back sir!! Please cover leetcode contests problems!!

  • @dhirajkumawat108
    @dhirajkumawat108 4 дні тому +3

    Wait a lot for your Video ❤❤

  • @sandeepvishwakarma8949
    @sandeepvishwakarma8949 3 дні тому +2

    we need yesterday 's solution by you only ❤❤

    • @codestorywithMIK
      @codestorywithMIK  3 дні тому +1

      Yes coming tomorrow for full details and many things to learn ❤️🙏

    • @codestorywithMIK
      @codestorywithMIK  3 дні тому +1

      Yes coming tomorrow with full details and extra things to learn ❤️🙏

  • @Coder_Buzz07
    @Coder_Buzz07 4 дні тому +3

    Mik bhaiya is back finally ❤❤

  • @VK_BRANDING
    @VK_BRANDING 3 дні тому +2

    return of the king

  • @aws_handles
    @aws_handles 4 дні тому +3

    MIK is back 😍
    Thanks for the clear explanation

  • @akashnaik5567
    @akashnaik5567 3 дні тому +2

    GOAT 🐐 Is back ❤

  • @vaibhav454
    @vaibhav454 3 дні тому +3

    Saal m kitne din ghumte ho mik bhai

  • @shrutigrover5092
    @shrutigrover5092 3 дні тому

    Great Video

  • @VarunSahu-tv5rj
    @VarunSahu-tv5rj 3 дні тому

    Bhiya codeforces contest ka solution discuss Kiya kro please aap chijo ko acha explain krte hai 🙏

  • @vallendsouza
    @vallendsouza 3 дні тому +3

    Sir, joh 2 din ka upload karna hai voh wale videos kab tak aayenge?

  • @dynamicvk2308
    @dynamicvk2308 3 дні тому +3

    Sir, your code of minimum_no_of_swaps function used in this code fails for one testcase in gfg problem: minimum no. of swaps to sort an array at last 1111th test case, could you please tell why it happen because it works correctly for leetcode, It would be very helpful sir

  • @jeehub041
    @jeehub041 4 дні тому +6

    Bhaiya is back ❤❤

  • @anonymoushackerar7507
    @anonymoushackerar7507 3 дні тому +1

    I used BFS + Cyclic Sort to solve this

    • @anonymoushackerar7507
      @anonymoushackerar7507 3 дні тому +1

      Here's my Solution:
      class Solution {
      public:
      int minSwapsToSort(vector& arr) {
      int n = arr.size();
      vector arrWithIndex(n);
      // Pair array elements with their indices
      for (int i = 0; i < n; i++) {
      arrWithIndex[i] = {arr[i], i};
      }
      // Sort the array by element values
      sort(arrWithIndex.begin(), arrWithIndex.end());
      vector visited(n, false); // To keep track of visited elements
      int swaps = 0;
      for (int i = 0; i < n; i++) {
      // If already visited or in the correct position, skip
      if (visited[i] || arrWithIndex[i].second == i) {
      continue;
      }
      // Count the size of the cycle
      int cycleSize = 0;
      int current = i;
      while (!visited[current]) {
      visited[current] = true;
      current = arrWithIndex[current]
      .second; // Move to the next index in the cycle
      cycleSize++;
      }
      // If there is a cycle of size > 1, it contributes (size - 1) swaps
      if (cycleSize > 1) {
      swaps += (cycleSize - 1);
      }
      }
      return swaps;
      }
      int minimumOperations(TreeNode* root) {
      // At Same Level, so we'll go with BFS
      queue q;
      q.push(root);
      int ans = 0;
      while (!q.empty()) {
      int size = q.size();
      vector treeNodeVals;
      for (int i = 0; i < size; i++) {
      TreeNode* temp = q.front();
      q.pop();
      if (temp->left != NULL) {
      treeNodeVals.push_back(temp->left->val);
      q.push(temp->left);
      }
      if (temp->right != NULL) {
      treeNodeVals.push_back(temp->right->val);
      q.push(temp->right);
      }
      }
      ans += minSwapsToSort(treeNodeVals);
      }
      return ans;
      }
      };

    • @amanpatel8575
      @amanpatel8575 3 дні тому

      @@anonymoushackerar7507 This actually works for GFG Problem: Minimum Swaps to Sort Array.

  • @rajashreeparhi1549
    @rajashreeparhi1549 4 дні тому +3

    I am solving questions, working hard..still I am only facing rejections..either my code doesn't run or ques becomes too tricky.... my luck doesn't play well...I am very demotivated.

  • @anonymoushackerar7507
    @anonymoushackerar7507 3 дні тому +1

    MIK Bhaiya, last 2 Days ki POTD ki video kab ayegi? 🥺🥺

  • @KeshavKumar-jh2en
    @KeshavKumar-jh2en 4 дні тому +1

    if time please leetcode contest.... dp on last sunday problem

  • @devlpr-nitish
    @devlpr-nitish 3 дні тому

    We missed you

  • @AbcDef-e1m1f
    @AbcDef-e1m1f 4 дні тому +4

    Bhaii please kl wala problem ka solution bna do
    Apna aadat lga diye ho bhaiii or kl solution nhi aaya 😢

    • @annagarg2567
      @annagarg2567 3 дні тому

      sahi bola😂, ab kisi aur se smjhn ka man nhi krta

  • @abhinay.k
    @abhinay.k 2 дні тому

    thanks

  • @331sachinkiragi8
    @331sachinkiragi8 3 дні тому

    Bro weekly contests ka atleast 3rd question ek kara do plz for every week

  • @tanmaychanda398
    @tanmaychanda398 2 дні тому

    how is storing in map and comparing with the sorted vector is making sure that the swaps are minimum??

  • @meetdobariya8777
    @meetdobariya8777 4 дні тому

    Last two days ke hard questions banao plz

  • @princesahu5356
    @princesahu5356 2 дні тому

    bhaiya no of swap dekhne ke liye kyu na ham given array ko sorted array se compare kr le like jis index pe number different honge us samay count++ kar denge and at last count/2 kar denge kyuki jis number se swap hua usme bhi to count++ hoga so is type se number of swap nikal jyega !!

  • @abhaymaurya9
    @abhaymaurya9 День тому

    ham 2nd type wala bfs kb lagate hai ? koi video hai aapki jismai specifically apne ye bataya ho?
    when we use first one and when we use 2nd one BFS
    aur agar ham hamesha size wala BFS use kre to galti hogi kya?

  • @Ununtag
    @Ununtag 3 дні тому +1

    Hi MIK, the function of "Minimum Swaps to sort" doesn't work and gives wrong answer on GFG site on the last test case. could you please let me know why?

    • @NitishRaj-q3s
      @NitishRaj-q3s 2 дні тому

      yes, i also facing the same problem.

  • @KishanSingh-vc3re
    @KishanSingh-vc3re 3 дні тому

    bhaiya ye sb to bn jata h, please thoda contests k b questions dekheye na, audience for that will be large too ! and we need it. please. the question MAXIMIZE AMOUNT AFTER TWO DAYS OF CONVERSION had me long to think and likewise.

  • @AyushGupta29164
    @AyushGupta29164 4 дні тому +2

    Bhaia maine abhi dsa seekhna start hi kara hain. please aap muje bata do ki main apke channel ka use kaise karun effeciently. main pahle ek topic ka theory dekh lu fir usi topic ki jo playlist apne banyi hain usme se questions karuun. Please thoda detail main samja do maine apka chaneel 1 week pahle hi discover kara hain itni saari vedios hain har playlist main mujhe dekh kar samaj nahi aa raha haiin kaise padhu unse

    • @codestorywithMIK
      @codestorywithMIK  3 дні тому +1

      First see this. It will help you get started -
      ua-cam.com/video/2Jshfog1ETg/v-deo.htmlsi=VWnPD-3-tjfcttT1

  • @ShivamSharma-vf4uf
    @ShivamSharma-vf4uf 2 дні тому

    SIR PLEASE SOLVE 21 DEC AND 22 DEC Problem of the day as you skipped these problems on that day @codestorywithMIK

  • @Your_Sandy07
    @Your_Sandy07 3 дні тому

    Ish BFS ka concept konsa video mein hain????

    • @gui-codes
      @gui-codes 3 дні тому +1

      aise to kai videos me bataya hai sir ne is BFS waali cheez ko but latest ek video aai thi sir ki "Reverse Odd Levels of Binary Tree" - Leetcode-2415 usme bataya tha fir se .

    • @Your_Sandy07
      @Your_Sandy07 3 дні тому

      @gui-codes thanks for ur information bro but BFS ka koi playlist hain kya???

    • @Tejas-Coder
      @Tejas-Coder 3 дні тому

      @@Your_Sandy07 Graph playlist me hain

  • @vishwashsoni610
    @vishwashsoni610 3 дні тому

    sir this is how i solved this question ...
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode *left;
    * TreeNode *right;
    * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    * };
    */
    class Solution {
    public:
    int minSwaps(vector& arr){
    int n = arr.size();
    vectornums;
    for(int i=0;ileft){
    pq.push(temp->left);
    }
    if(temp->right){
    pq.push(temp->right);
    }
    }
    result += minSwaps(arr);

    }
    return result;
    }
    };

  • @jayanaaryan3498
    @jayanaaryan3498 3 дні тому

    bhaiya kal aur purso ka potd upload kar samjh me nhi aaya

  • @vibhanshusharma9143
    @vibhanshusharma9143 3 дні тому

    But but but what if àrray contain duplicate value 😅.
    If It has better time complexity than n*n . Plz tell me😊

  • @DevanshGupta-io7rl
    @DevanshGupta-io7rl 4 дні тому

    int solve(vector &v){
    int swaps=0,n=v.size();
    vectortemp=v;
    sort(begin(temp),end(temp));
    unordered_mapmp;
    for(int i=0;ileft);
    if(it->right) q.push(it->right);
    }
    ans+=solve(v);
    }
    return ans;
    }
    my solution. important cheez thi min swaps to get array sorted jo wo decode kr liya smjho question ho gya.. or yahi maine codeforces pe solve kri thi..

  • @itsharshita22
    @itsharshita22 3 дні тому

    Bhaiya mene ye code khud se likha h or jab dry run kia tha to ye code 3ino test cases k liye pass ho rha tha pr jab editor pr run kia to ye ek b test cases pass ni kr pa rha . Kya ap bta sakte h is question ko ese kyu ni kr sakte
    Code -->
    int minimumOperations(TreeNode* root) {
    int count = 0;
    queue que;
    que.push(root);
    while (!que.empty()) {
    int n = que.size();
    vector vec;
    while (n--) {
    auto curr = que.front();
    que.pop();
    vec.push_back(curr);
    if (curr->left != NULL) {
    que.push(curr->left);
    }
    if (curr->right != NULL) {
    que.push(curr->right);
    }
    }
    int l = 0;
    int r = vec.size() - 1;
    while (l < r) {
    if (vec[l]->val > vec[r]->val) {
    auto temp = vec[l]->val;
    vec[l]->val = vec[r]->val;
    vec[r]->val = temp;
    l++;
    count++;
    } else {
    r--;
    }
    }
    }
    return count;
    }

    • @gui-codes
      @gui-codes 3 дні тому

      can you share which test case is failing ?

  • @learn_in_shorts
    @learn_in_shorts 4 дні тому +1

    Hello bhaiya kaise ho aap,
    Bhaiya mujhse ek help chahiye thi aapse plz 🙏 meyne bahut try Kiya but kahi nhi hua, bhaiya final year ke liye project banana hai to uske liye pahle guide registration karna hai and mujhse koi nahi mil rha hai, requirement for guide is ( btech + 5 year experience in job / teaching). Bhaiya aap kisi ko bologe ya phir aapke contact mey koi hoto plz contact kardo, college mey jab tak guide registration nahi karunga tab tak aage proceed nhi kar sakta, bas formality karna hai bhaiya only guide ka details submit karna hai bas itna plz bhaiya help me 🙏🙏🙏🙏

  • @FanIQQuiz
    @FanIQQuiz 3 дні тому +1

    You are really good 🫡