You are viewing a single comment's thread. Return to all comments →
python
def almostSorted(arr): idxList = list() sortedArr = sorted(arr) for i, n in enumerate(arr): if n != sortedArr[i]: idxList.append(i) if len(idxList) == 0: print("yes") elif len(idxList) == 2: print("yes") print(f"swap {idxList[0]+1} {idxList[-1]+1}") else: if arr[idxList[0]:idxList[-1]+1] == sorted(arr[idxList[0]:idxList[-1]+1], reverse=True): print("yes") print(f"reverse {idxList[0]+1} {idxList[-1]+1}") else: print("no")
Seems like cookies are disabled on this browser, please enable them to open this website
Almost Sorted
You are viewing a single comment's thread. Return to all comments →
python