Lily's Homework

  • + 1 comment

    python3

    def removeOverlapping(arr, sortedArr):
        numToIdx = {n: idx for n, idx in zip(arr, range(len(arr)))}
        swaps = 0
        
        for sIdx in range(len(sortedArr)):
            if sortedArr[sIdx] == arr[sIdx]:
                continue
            else:
                swappedOutItem = arr[sIdx]
                arr[sIdx] = sortedArr[sIdx]
                arr[numToIdx[sortedArr[sIdx]]] = swappedOutItem
                numToIdx[swappedOutItem] = numToIdx[sortedArr[sIdx]]
                numToIdx[sortedArr[sIdx]] = sIdx
                swaps += 1
        return swaps
            
    def lilysHomework(arr):
        sortedArr = sorted(arr, reverse=True)
        return min(removeOverlapping(list(arr), sortedArr) , removeOverlapping(arr, list(reversed(sortedArr))))