You are viewing a single comment's thread. Return to all comments →
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))))
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
python3