You are viewing a single comment's thread. Return to all comments →
def largestPermutation(k, arr) array_swap = Array.new(arr.length) arr.length.times { |i| array_swap[arr[i]] = i} arr.length.times do |i| next if arr[i] == arr.length-i if k > 0 index_change = array_swap[arr.length-i] array_swap[arr[i]] = index_change arr[i], arr[index_change] = arr[index_change], arr[i] k -= 1 end end arr end
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 →
Ruby