Created
January 8, 2022 10:43
-
-
Save nhnam/5a5ba2d900590fdd7c9da6ffce6dd7d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Complete the minimumSwaps function below. | |
function minimumSwaps(arr) { | |
var swaps = 0; | |
for(var i=0; i< arr.length; i++) { | |
while(arr[i] !== i + 1) { | |
let j = arr[i] - 1; | |
let tem = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = tem; | |
swaps ++; | |
} | |
} | |
return swaps; | |
} | |
let res = minimumSwaps([3, 7, 6, 9, 1, 8, 10, 4, 2, 5]); | |
res; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment