You are viewing a single comment's thread. Return to all comments →
simple c++ code with function
int getswaps(unordered_map& d) { int swap = 0; for (auto& pair : d) { int x = pair.first; int y = pair.second; while (x != y) { swap++; d[x] = d[y]; d[y] = y; y = d[x]; } } return swap; } int lilysHomework(vector arr) { vector arrs = arr; sort(arrs.begin(), arrs.end()); unordered_map d1, d2;
for (size_t i = 0; i < arr.size(); i++) { d1[arr[i]] = arrs[i]; d2[arr[i]] = arrs[arr.size() - 1 - i]; } return min(getswaps(d1), getswaps(d2));
}
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
simple c++ code with function
int getswaps(unordered_map& d) { int swap = 0; for (auto& pair : d) { int x = pair.first; int y = pair.second; while (x != y) { swap++; d[x] = d[y]; d[y] = y; y = d[x]; } } return swap; } int lilysHomework(vector arr) { vector arrs = arr; sort(arrs.begin(), arrs.end()); unordered_map d1, d2;
}