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 🔥🔥🔥

  • @amanpaliwalvlogs6860
    @amanpaliwalvlogs6860 3 วันที่ผ่านมา +3

    Radhe Radhe ❤❤

  • @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 4 วันที่ผ่านมา +5

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

  • @ansh6552
    @ansh6552 4 วันที่ผ่านมา +8

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

  • @ravirathore6717
    @ravirathore6717 4 วันที่ผ่านมา +5

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

  • @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.

  • @shraban8508
    @shraban8508 4 วันที่ผ่านมา +3

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

  • @robot3.077
    @robot3.077 4 วันที่ผ่านมา +7

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

  • @Coder_Buzz07
    @Coder_Buzz07 4 วันที่ผ่านมา +3

    Mik bhaiya is back finally ❤❤

  • @dhirajkumawat108
    @dhirajkumawat108 4 วันที่ผ่านมา +3

    Wait a lot for your Video ❤❤

    • @gui-codes
      @gui-codes 4 วันที่ผ่านมา

      same

  • @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 ❤️🙏

  • @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 4 วันที่ผ่านมา +3

    Saal m kitne din ghumte ho mik bhai

  • @jeehub041
    @jeehub041 4 วันที่ผ่านมา +6

    Bhaiya is back ❤❤

  • @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 🙏

  • @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

  • @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.

  • @vallendsouza
    @vallendsouza 4 วันที่ผ่านมา +3

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

  • @anonymoushackerar7507
    @anonymoushackerar7507 4 วันที่ผ่านมา +1

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

  • @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 !!

  • @anonymoushackerar7507
    @anonymoushackerar7507 4 วันที่ผ่านมา +1

    I used BFS + Cyclic Sort to solve this

    • @anonymoushackerar7507
      @anonymoushackerar7507 4 วันที่ผ่านมา +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.

  • @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

  • @KeshavKumar-jh2en
    @KeshavKumar-jh2en 4 วันที่ผ่านมา +1

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

  • @tanmaychanda398
    @tanmaychanda398 3 วันที่ผ่านมา

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

  • @abhaymaurya9
    @abhaymaurya9 2 วันที่ผ่านมา

    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?

  • @abhinay.k
    @abhinay.k 3 วันที่ผ่านมา

    thanks

  • @ShivamSharma-vf4uf
    @ShivamSharma-vf4uf 3 วันที่ผ่านมา

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

  • @meetdobariya8777
    @meetdobariya8777 4 วันที่ผ่านมา

    Last two days ke hard questions banao plz

  • @331sachinkiragi8
    @331sachinkiragi8 3 วันที่ผ่านมา

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

  • @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 3 วันที่ผ่านมา

      yes, i also facing the same problem.

  • @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  4 วันที่ผ่านมา +1

      First see this. It will help you get started -
      th-cam.com/video/2Jshfog1ETg/w-d-xo.htmlsi=VWnPD-3-tjfcttT1

  • @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

  • @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.

  • @vishwashsoni610
    @vishwashsoni610 4 วันที่ผ่านมา

    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;
    }
    };

  • @vibhanshusharma9143
    @vibhanshusharma9143 4 วันที่ผ่านมา

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

  • @jayanaaryan3498
    @jayanaaryan3498 3 วันที่ผ่านมา

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

    • @codestorywithMIK
      @codestorywithMIK  3 วันที่ผ่านมา

      Coming tomorrow with details ❤️

  • @itsharshita22
    @itsharshita22 4 วันที่ผ่านมา

    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 ?

  • @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..

  • @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 🫡