You are viewing a single comment's thread. Return to all comments →
Kotlin solution
fun minimumSwaps(arr: Array<Int>): Int { val list = arr.toMutableList() var swapCount = 0 for (i in arr.lastIndex downTo 0){ val value = list[i] val sortedValue = (i + 1) if(value == sortedValue){ list.removeLast() continue } val swapTargetIndex = list.indexOf(sortedValue) list[swapTargetIndex] = value list.removeLast() swapCount++ } return swapCount }
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
Kotlin solution