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

  Рет қаралды 6,095

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер
@gamerboy188
@gamerboy188 19 күн бұрын
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 19 күн бұрын
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 19 күн бұрын
@@gui-codes we got this bro , let's gooooo
@arnabsarkar5245
@arnabsarkar5245 19 күн бұрын
All the best buddy.
@aws_handles
@aws_handles 19 күн бұрын
Let’s do this 🔥🔥🔥
@dibbodas4116
@dibbodas4116 19 күн бұрын
finally after few days mik sir is back✅✅
@gui-codes
@gui-codes 19 күн бұрын
The LEGEND is back 🔥 Waited eagerly to see your videos.
@amanpaliwalvlogs6860
@amanpaliwalvlogs6860 18 күн бұрын
Radhe Radhe ❤❤
@ansh6552
@ansh6552 19 күн бұрын
Finally MIK sir is back ❤️ Sir ho sake to kal wala potd karado copy paste kiya tha kal wala 😅
@ravirathore6717
@ravirathore6717 19 күн бұрын
I solved it today on my own, thanks for boosting my confidence.
@parulawasthi7353
@parulawasthi7353 18 күн бұрын
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.
@Believe_yourself-eo1ho
@Believe_yourself-eo1ho 19 күн бұрын
I am done BRUTE FORCE BFS + Selection Sort Thank you you are back ❤❤❤❤ Please continue DP CONCEPT WALA PLAYLIST 🙏🙏🙏
@arohi3277
@arohi3277 14 күн бұрын
can you give me solution pls
@Believe_yourself-eo1ho
@Believe_yourself-eo1ho 14 күн бұрын
@arohi3277 int SelectionSort(vector&temp){ int idx = 0; int n = temp.size(); int No_of_Operation = 0; for(int i=0;ileft); if(front->right) q.push(front->right); } operation += SelectionSort(temp); } return operation; }
@Believe_yourself-eo1ho
@Believe_yourself-eo1ho 14 күн бұрын
@@arohi3277 int SelectionSort(vector&temp){ int idx = 0; int n = temp.size(); int No_of_Operation = 0; for(int i=0;ileft); if(front->right) q.push(front->right); } operation += SelectionSort(temp); } return operation; }
@shraban8508
@shraban8508 19 күн бұрын
Finally you are back sir!! Please cover leetcode contests problems!!
@VK_BRANDING
@VK_BRANDING 19 күн бұрын
return of the king
@Coder_Buzz07
@Coder_Buzz07 19 күн бұрын
Mik bhaiya is back finally ❤❤
@dhirajkumawat108
@dhirajkumawat108 19 күн бұрын
Wait a lot for your Video ❤❤
@gui-codes
@gui-codes 19 күн бұрын
same
@jeehub041
@jeehub041 19 күн бұрын
Bhaiya is back ❤❤
@sandeepvishwakarma8949
@sandeepvishwakarma8949 19 күн бұрын
we need yesterday 's solution by you only ❤❤
@codestorywithMIK
@codestorywithMIK 19 күн бұрын
Yes coming tomorrow for full details and many things to learn ❤️🙏
@codestorywithMIK
@codestorywithMIK 19 күн бұрын
Yes coming tomorrow with full details and extra things to learn ❤️🙏
@akashcodeeerrrr
@akashcodeeerrrr 19 күн бұрын
GOAT 🐐 Is back ❤
@aws_handles
@aws_handles 19 күн бұрын
MIK is back 😍 Thanks for the clear explanation
@dynamicvk2308
@dynamicvk2308 18 күн бұрын
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
@robot3.077
@robot3.077 19 күн бұрын
Sir kal ke potd ki ek video solution bana dena. wo infosys ke interview me poochaa gaya tha
@iamnoob7593
@iamnoob7593 5 күн бұрын
what infosys is asking leetcode medium questions??
@robot3.077
@robot3.077 4 күн бұрын
@@iamnoob7593 bro for specialist programmer role 9.5 lpa
@iamnoob7593
@iamnoob7593 5 күн бұрын
Amazing explanation , Is it modified selection sort , If array had duplicates , we may have to go with another approach for finding min swaps.
@shrutigrover5092
@shrutigrover5092 19 күн бұрын
Great Video
@anonymoushackerar7507
@anonymoushackerar7507 19 күн бұрын
I used BFS + Cyclic Sort to solve this
@anonymoushackerar7507
@anonymoushackerar7507 19 күн бұрын
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 18 күн бұрын
@@anonymoushackerar7507 This actually works for GFG Problem: Minimum Swaps to Sort Array.
@rajashreeparhi1549
@rajashreeparhi1549 19 күн бұрын
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.
@vaibhav454
@vaibhav454 19 күн бұрын
Saal m kitne din ghumte ho mik bhai
@princesahu5356
@princesahu5356 18 күн бұрын
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 !!
@devlpr-nitish
@devlpr-nitish 19 күн бұрын
We missed you
@tanmaychanda398
@tanmaychanda398 18 күн бұрын
how is storing in map and comparing with the sorted vector is making sure that the swaps are minimum??
@abhinay.k
@abhinay.k 18 күн бұрын
thanks
@VarunSahu-tv5rj
@VarunSahu-tv5rj 19 күн бұрын
Bhiya codeforces contest ka solution discuss Kiya kro please aap chijo ko acha explain krte hai 🙏
@vallendsouza
@vallendsouza 19 күн бұрын
Sir, joh 2 din ka upload karna hai voh wale videos kab tak aayenge?
@KeshavKumar-jh2en
@KeshavKumar-jh2en 19 күн бұрын
if time please leetcode contest.... dp on last sunday problem
@abhaymaurya9
@abhaymaurya9 17 күн бұрын
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?
@anonymoushackerar7507
@anonymoushackerar7507 19 күн бұрын
MIK Bhaiya, last 2 Days ki POTD ki video kab ayegi? 🥺🥺
@AbcDef-e1m1f
@AbcDef-e1m1f 19 күн бұрын
Bhaii please kl wala problem ka solution bna do Apna aadat lga diye ho bhaiii or kl solution nhi aaya 😢
@annagarg2567
@annagarg2567 19 күн бұрын
sahi bola😂, ab kisi aur se smjhn ka man nhi krta
@ManojKrVerma-vw4dx
@ManojKrVerma-vw4dx 11 күн бұрын
Hi MIK, I am not sure ki ye swap count min hi kyun hai ? How is it ensuring ki issie kam swaps mei ye achieve nahi ho sakta. Please help reply.
@331sachinkiragi8
@331sachinkiragi8 18 күн бұрын
Bro weekly contests ka atleast 3rd question ek kara do plz for every week
@KishanSingh-vc3re
@KishanSingh-vc3re 18 күн бұрын
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.
@ShivamSharma-vf4uf
@ShivamSharma-vf4uf 18 күн бұрын
SIR PLEASE SOLVE 21 DEC AND 22 DEC Problem of the day as you skipped these problems on that day @codestorywithMIK
@Ununtag
@Ununtag 18 күн бұрын
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 18 күн бұрын
yes, i also facing the same problem.
@meetdobariya8777
@meetdobariya8777 19 күн бұрын
Last two days ke hard questions banao plz
@AyushGupta29164
@AyushGupta29164 19 күн бұрын
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 19 күн бұрын
First see this. It will help you get started - kzbin.info/www/bejne/aHvWmZmlnJZ4isksi=VWnPD-3-tjfcttT1
@vishwashsoni610
@vishwashsoni610 19 күн бұрын
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; } };
@codestorywithMIK
@codestorywithMIK 19 күн бұрын
Well done. Clean code 👍🏻
@Your_Sandy07
@Your_Sandy07 19 күн бұрын
Ish BFS ka concept konsa video mein hain????
@gui-codes
@gui-codes 19 күн бұрын
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 19 күн бұрын
@gui-codes thanks for ur information bro but BFS ka koi playlist hain kya???
@Tejas-Coder
@Tejas-Coder 18 күн бұрын
@@Your_Sandy07 Graph playlist me hain
@DevanshGupta-io7rl
@DevanshGupta-io7rl 19 күн бұрын
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..
@jayanaaryan3498
@jayanaaryan3498 18 күн бұрын
bhaiya kal aur purso ka potd upload kar samjh me nhi aaya
@codestorywithMIK
@codestorywithMIK 18 күн бұрын
Coming tomorrow with details ❤️
@vibhanshusharma9143
@vibhanshusharma9143 19 күн бұрын
But but but what if àrray contain duplicate value 😅. If It has better time complexity than n*n . Plz tell me😊
@itsharshita22
@itsharshita22 19 күн бұрын
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 19 күн бұрын
can you share which test case is failing ?
@learn_in_shorts
@learn_in_shorts 19 күн бұрын
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 19 күн бұрын
You are really good 🫡
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 146 М.
What Nobody Tells You About Coinbase Interview Rounds!
13:26
codeWithAryan
Рет қаралды 19 М.
Pick these tech to make or destroy your tech career
15:52
Chai aur Code
Рет қаралды 124 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 719 М.
Find Largest Value in Each Tree Row | DFS | BFS | META | Leetcode - 515
22:53