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.
And yet a much simpler approach, using no map, no extra data structures at all, will pass. On paper its O=n^2, yet its quick enough to not time out and pass all tests. Just search with an inner loop from i+1 until you find it -- if it was before you'd have already swapped, and just be sure to break the inner loop when you find the swap.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
And yet a much simpler approach, using no map, no extra data structures at all, will pass. On paper its O=n^2, yet its quick enough to not time out and pass all tests. Just search with an inner loop from i+1 until you find it -- if it was before you'd have already swapped, and just be sure to break the inner loop when you find the swap.