We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Minimum Swaps 2
- Discussions
Minimum Swaps 2
Minimum Swaps 2
Sort by
recency
|
2470 Discussions
|
Please Login in order to post a comment
I noticed that in Julia the code raises error for STDIN written with capital letters. Once I changed it to stdin, everything went smooth. So, I guess you should update the current version (unless I am missing something).
Python code.
Final ordered array should look like [1, 2, 3, ...], so we can iterate through the array and check if current value matches index+1. If they do not match, find the index of the value that matches index+1. Then swap the values at the current index and the other index and record the swap.
Note: this code wouldn't work if the array isn't consecutive but it seems like all the test case arrays are consecutive now.
I though might be helpfull to someone..
My JavaScript (JS/node.js) solution. This assumes the first number may not start at 1.