You are viewing a single comment's thread. Return to all comments →
def largestPermutation(k, arr): indices = {value: index for index, value in enumerate(arr)} n = len(arr) maxValue = n for i in range(n): if k == 0: break if arr[i] != maxValue: maxIndex = indices[maxValue] indices[arr[i]], indices[maxValue] = indices[maxValue], indices[arr[i]] arr[i], arr[maxIndex] = arr[maxIndex], arr[i] k -= 1 maxValue -= 1 return arr
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Permutation
You are viewing a single comment's thread. Return to all comments →