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

preview_player
Показать описание
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)
Рекомендации по теме
Комментарии
Автор

Very nicely explained, clarity in your voice. This code is easy to understand than GFG solution. Good job!

intuitive_coder
Автор

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!

rishukumarsingh
Автор

Very nicely explained..keep up the good work and keep coming up with such videos, and of course thanks a ton

arijitdey
Автор

Why is the result minimal ? That explanation is not clear. Might be better to explain using dependency graph and connected components

kaushikvelidandla
Автор

such logics are hard to build. hatsoff

sougatoghosh
Автор

Great explanation & conceptual clarity!

nayankhanna
Автор

Irony is that, this cannot be solved without sorting the array 😂

pratube
Автор

thank you for putting so much efforts, worth the time 👍

yuvrajagarkar
Автор

in which company are you placed now and with what approx package?
(you are great motivation)

hardcash
Автор

Will this appraoch work for if elements are from 0 - N-1

kirankutte
Автор

Thanks, but how can we claim this approach always guarantee the correct ans?
Is there some intuition behind it?

shyren_more
Автор

How can we tackle this if duplicates are allowed?

techstuff
Автор

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

dhoni-
Автор

Mam how do we make sure its the minimum number of sorts which we had found

shreyamaheshwari
Автор

I came here to see graph approach why using wrong thumbnail, but your approach is pretty much hint to the graph approach

Jitendrakumar-gbcn
Автор

What is the intuition behind this approach??

anshulkumarneekhara
Автор

do this method have a name ?
if it is pls tell the name

prathammittal
Автор

You look so gorgeous btw nice explanation ❤

kennyackerman
Автор

Woah.. how to come up with ideas like this...

systemforge
Автор

Let me just ignore it was the best part😄

aryansiroyajain
visit shbcf.ru