A browser extension reorders open tabs by their numeric IDs, ascending. Moving tabs is slow, so it only uses swaps: one swap exchanges the positions of any two tabs, not just neighbors.
Write minSwaps(ids) that returns the smallest number of swaps that sorts ids ascending. All IDs are different.
Input: ids = [4,3,1,2] Output: 3
Each tab is in another tab's spot, forming one loop of 4. A loop of 4 needs 3 swaps.
Input: ids = [1,5,4,3,2] Output: 2
Swap 5 with 2, then 4 with 3.
0 <= ids.length <= 3 * 10^5-10^9 <= ids[i] <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #187 Fewest Swaps to Order Tabs. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.