Minimum Swaps to sort intuition + code C++ with explanation

  Рет қаралды 21,380

Code with Alisha

Code with Alisha

2 жыл бұрын

Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
Example 1:
Input:
nums = {2, 8, 5, 4}
Output:
1
Explaination:
swap 8 with 4.
Example 2:
Input:
nums = {10, 19, 6, 3, 5}
Output:
2
Explaination:
swap 10 with 3 and swap 19 with 5.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
Expected Time Complexity: O(nlogn)
Expected Auxiliary Space: O(n)

Пікірлер: 49
@arijitdey8419
@arijitdey8419 2 жыл бұрын
Very nicely explained..keep up the good work and keep coming up with such videos,and of course thanks a ton
@i_DeepakV
@i_DeepakV 2 жыл бұрын
Very nicely explained, clarity in your voice. This code is easy to understand than GFG solution. Good job!
@pratube4846
@pratube4846 2 жыл бұрын
Irony is that, this cannot be solved without sorting the array 😂
@vivekshrivastav3674
@vivekshrivastav3674 Жыл бұрын
it can be done but that method will take O(n^2) time complexity. Selection sort
@aj.anshuljohri
@aj.anshuljohri 11 ай бұрын
@@vivekshrivastav3674 You can optimize the selection using map. I did this, it worked, and it's O(nlogn). int minSwaps(vector &num) { // Code here map mp; int ans = 0; int n = num.size(); for (int i = 0; i < n; i++) { //assigning index to its value mp[num[i]] = i; } for (int i = 0; i < n; i++) { //extracting minimum element auto pr = *mp.begin(); int mini_ele = pr.first; int mini_ele_ind = pr.second; if (mini_ele_ind != i) { //swapping current element index with //minimum element index mp[num[i]] = mini_ele_ind; swap(num[i], num[mini_ele_ind]); ans++; } //erasing the minimum element as it's processed mp.erase(mp.begin()); } return ans; }
@barathnatarajan8566
@barathnatarajan8566 2 жыл бұрын
Beautiful explanation So clear!
@rishukumarsingh3926
@rishukumarsingh3926 Жыл бұрын
I understood the approach, but i don't get that, what lead to this approach, what is the thought process behind appraching this question in this ways, when i was solving it, i made it complicated by observing dependencies of position in terms of graph!
@adeeshsinghai7105
@adeeshsinghai7105 6 күн бұрын
LET CASE 1: Bhai agr kisi array ko sort krna h toh number of swaps btane hain. LET CASE 2: Maan le tune uss array ko sort kr diya ab tu chahta h uss array ko wapas se original array m convert krna . Dono case m swaps ka answer same hoga. BASS LOGIC YAHI H. TRACK KRTE JAA RAHE HAIN CORRECT POSITION KYA HONI CHAHIYE SORTED ARRAY MEIN
@yuvrajagarkar8942
@yuvrajagarkar8942 Жыл бұрын
thank you for putting so much efforts, worth the time 👍
@vamshisamineniz5905
@vamshisamineniz5905 2 жыл бұрын
good selection of questions
@sougatoghosh8467
@sougatoghosh8467 2 жыл бұрын
such logics are hard to build. hatsoff
@kennyackerman5429
@kennyackerman5429 4 ай бұрын
You look so gorgeous btw nice explanation ❤
@ashokdurunde1814
@ashokdurunde1814 2 жыл бұрын
very nice explanation thank you very much
@hardcash9930
@hardcash9930 Жыл бұрын
in which company are you placed now and with what approx package? (you are great motivation)
@indiancyberclub
@indiancyberclub Жыл бұрын
nice explaination absolutely loved it❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
@kaushikvelidandla7010
@kaushikvelidandla7010 2 жыл бұрын
Why is the result minimal ? That explanation is not clear. Might be better to explain using dependency graph and connected components
@kanaramjangid8563
@kanaramjangid8563 3 күн бұрын
Very nice
@MritunjayKumar-rd2es
@MritunjayKumar-rd2es Жыл бұрын
this was the best explaination
@sangamchoudhary6977
@sangamchoudhary6977 2 жыл бұрын
fantastic 🔥
@rishikeshmishra715
@rishikeshmishra715 9 ай бұрын
Nice explanation !
@piyushbhagwat3119
@piyushbhagwat3119 Жыл бұрын
Thank you!
@techstuff9830
@techstuff9830 Жыл бұрын
How can we tackle this if duplicates are allowed?
@somnathmore1082
@somnathmore1082 2 жыл бұрын
Thank you didi,clear my doubt
@tit162-tiwarianurag2
@tit162-tiwarianurag2 2 жыл бұрын
Explanation is good
@kirankutte7073
@kirankutte7073 2 жыл бұрын
Will this appraoch work for if elements are from 0 - N-1
@pk4288
@pk4288 11 ай бұрын
what if there are repeated elemets
@debashishghosh4549
@debashishghosh4549 2 жыл бұрын
Can somebody tell me the difference between the number of inversion count question and this one.
@anshulkumarneekhara9377
@anshulkumarneekhara9377 3 ай бұрын
What is the intuition behind this approach??
@manojnavinjamuri5867
@manojnavinjamuri5867 Жыл бұрын
Thank you Alisha
@AmanSharma-vb5jl
@AmanSharma-vb5jl 2 жыл бұрын
🔥
@systemforge
@systemforge 2 жыл бұрын
Woah.. how to come up with ideas like this...
@harshits-1195
@harshits-1195 11 ай бұрын
Can anybody Tell me What if there are duplicate elements in vector
@shyren_more
@shyren_more Жыл бұрын
Thanks, but how can we claim this approach always guarantee the correct ans? Is there some intuition behind it?
@anshulkumarneekhara9377
@anshulkumarneekhara9377 3 ай бұрын
correct. What is the intuition behind this approach??
@abc-ym4zs
@abc-ym4zs 9 ай бұрын
madam i have one doubt when i have seen in solution in discussion section i am not able to understand but after watching your youtube solution i understood so why this happens i am always depending on yt videos how to overcome this i am not able to improve problem solving
@dhananjoydey1337
@dhananjoydey1337 2 жыл бұрын
thanks
@aryansiroyajain8503
@aryansiroyajain8503 2 жыл бұрын
Let me just ignore it was the best part😄
@shreyamaheshwari7329
@shreyamaheshwari7329 2 жыл бұрын
Mam how do we make sure its the minimum number of sorts which we had found
@bhaskarmishra8479
@bhaskarmishra8479 2 жыл бұрын
yeah... how do we guarantee that this would be the min no. of swaps
@T_tintin
@T_tintin Жыл бұрын
I think it's probably the minimum no of swaps becoz we r sorting the array and we already know the correct positions of the elements and we r placing them there only . It's the best thing we can do .Put them where they should be with one swap only. And if they r already there don't swap.
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
good video
@smartwork7098
@smartwork7098 Жыл бұрын
Thanks
@learnwithayush7838
@learnwithayush7838 Жыл бұрын
😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍 Very Nice Explanation . And you are also very beutifull😁😁😁😁
@Jitendrakumar-gb7cn
@Jitendrakumar-gb7cn 2 жыл бұрын
I came here to see graph approach why using wrong thumbnail , but your approach is pretty much hint to the graph approach
@alexrcrew1975
@alexrcrew1975 Жыл бұрын
one day her browser is gonna crash in between the tutorial .. so many tabs are opened haha
@apk7373
@apk7373 2 жыл бұрын
Use a better writing board like microsoft whiteboard or any similar... The one you are using is worse. But the explanation is amazing
@deepakjainseth8126
@deepakjainseth8126 2 жыл бұрын
alisha aap 1x ke speed mey bola karo
@Ninja-yt2qs
@Ninja-yt2qs 2 жыл бұрын
Very confusing, no need to complex the simple things
@prakashsingh1107
@prakashsingh1107 Жыл бұрын
Itna jldi bolti ho ki kuch smjh hi ni aata
HackerRank Interview Prep Kit - Problem 8: Minimum Swaps 2
14:52
Search in Rotated Sorted Array  #Binary Search Rotations Leetcode 33.
21:09
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 38 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 29 МЛН
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 1,5 МЛН
Leetcode 3. Longest Substring Without Repeating Characters
15:49
Code with Alisha
Рет қаралды 64 М.
Diffie-Hellman Key Exchange: How to Share a Secret
9:09
Spanning Tree
Рет қаралды 139 М.
Merge intervals #Leetcode #Interviewbit C++
10:24
Code with Alisha
Рет қаралды 26 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 808 М.
When King BREAKS The 4th WALL | Chess Memes #190
8:36
Top Chess
Рет қаралды 96 М.
Next Permutation || LeetCode #31 || Medium || Code + Explanation || C++
11:27
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 38 МЛН