You are viewing a single comment's thread. Return to all comments →
Here is my python 3 solution. Improved version of mwwhite's solution.
def minimumSwaps(arr): res = 0 arr = [x-1 for x in arr] value_idx = {x:i for i, x in enumerate(arr)} for i, x in enumerate(arr): if i != x: to_swap_idx = value_idx[i] arr[i], arr[to_swap_idx] = i, x value_idx[i] = i value_idx[x] = to_swap_idx res += 1 print(res) return res
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 →
Here is my python 3 solution. Improved version of mwwhite's solution.