Google | Onsite | Min swaps to make sorted

Give you an array, all elements in this array are unique. You can only use swap operation. What is the minimum steps you will need to make this array in ascending order? And print path.

Example 1:

Input: [1, 2, 3, 4]
Output: {} means empty path
Explanation: Array s already in ascending order so we don't need to swap anything.

Example 2:

Input: [3, 4, 1, 2]
Output: 2
Explanation [3, 4, 1, 2] -> [1, 4, 3, 2] -> [1, 2, 3, 4]

I used bfs, can anyone give me other solutions? Thanks a lot.

Comments (23)