You are viewing a single comment's thread. Return to all comments →
My cpp solution is here :
int minimumSwaps(vector<int> arr) { vector<int> sorted{arr}; sort(sorted.begin(),sorted.end()); int swapCount{0}; for (int i=0; i < sorted.size(); i++) { if(sorted[i] != arr[i]) { auto result = std::find(arr.begin()+i, arr.end(), sorted[i]); int index = std::distance(arr.begin(), result); int swap = arr[i]; arr[i] = arr[index]; arr[index] = swap; swapCount++; } } return swapCount; }
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
My cpp solution is here :