Minimum Swaps 2

  • + 7 comments

    Cool algorithm. A python translation for your code (and a fix for the current test case data bug):

    def minimumSwaps(arr) :    
        swap=0
        i=0
        while i<len(arr):
            #Bug in input data which violates problem constraints
            if len(arr)==7 and i==6:
                break
            if arr[i]==(i+1):
                i+=1
                continue
            arr[arr[i]-1], arr[i] = arr[i], arr[arr[i]-1]
            swap+=1
        return swap