You are viewing a single comment's thread. Return to all comments →
Hi CompSocialSci, My code is below and quite similar to yours. But, I got "terminated due to timedout" error in some of the test case.
Can you please shed the light on why it is slower? Much appreciated.
def minimumSwaps(arr): swapcount = 0 idx = 0 for i in range(len(arr)-1): if arr[i] != i+1: idx = arr.index(i+1) arr[i], arr[idx] = arr[idx], arr[i] swapcount += 1 return swapcount
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 →
Hi CompSocialSci, My code is below and quite similar to yours. But, I got "terminated due to timedout" error in some of the test case.
Can you please shed the light on why it is slower? Much appreciated.
def minimumSwaps(arr): swapcount = 0 idx = 0 for i in range(len(arr)-1): if arr[i] != i+1: idx = arr.index(i+1) arr[i], arr[idx] = arr[idx], arr[i] swapcount += 1 return swapcount