|
| 1 | + |
| 2 | +// Original Link |
| 3 | +// https://www.hackerrank.com/challenges/minimum-swaps-2/problem |
| 4 | + |
| 5 | +#include <bits/stdc++.h> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +vector<string> split_string(string); |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +///////////////// SOLUTION ////////////////// |
| 15 | + |
| 16 | +// Idea is to solve it with a recurrent function which iteratively performs greedy swaps |
| 17 | + |
| 18 | +void swap(vector<int>& v, const unsigned int a, const unsigned int b) |
| 19 | +{ |
| 20 | + int temp; |
| 21 | + temp = v[a]; |
| 22 | + v[a] = v[b]; |
| 23 | + v[b] = temp; |
| 24 | +} |
| 25 | + |
| 26 | +unsigned int search(const vector<int>& v, const unsigned int p, const int q) |
| 27 | +{ |
| 28 | + for(unsigned int i=p; i<v.size(); ++i) if(v[i] == q) return i; |
| 29 | + throw std::runtime_error("Not Found"); |
| 30 | +} |
| 31 | + |
| 32 | +int solve(vector<int>& v, const unsigned int i, const unsigned int cont) |
| 33 | +{ |
| 34 | + // Recursion Ending Conditions |
| 35 | + if(i == v.size()-1) return cont; |
| 36 | + |
| 37 | + // Expected Number |
| 38 | + const int n = i+1; |
| 39 | + if(v[i] == n) return solve(v, i+1, cont); |
| 40 | + else |
| 41 | + { |
| 42 | + const unsigned int j = search(v, i+1, n); |
| 43 | + swap(v, i, j); |
| 44 | + return solve(v, i+1, cont+1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Complete the minimumSwaps function below. |
| 49 | +int minimumSwaps(vector<int> arr) { |
| 50 | + return solve(arr, 0, 0); |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +///////////////////////////////////////// |
| 57 | + |
| 58 | +int main() |
| 59 | +{ |
| 60 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 61 | + |
| 62 | + int n; |
| 63 | + cin >> n; |
| 64 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 65 | + |
| 66 | + string arr_temp_temp; |
| 67 | + getline(cin, arr_temp_temp); |
| 68 | + |
| 69 | + vector<string> arr_temp = split_string(arr_temp_temp); |
| 70 | + |
| 71 | + vector<int> arr(n); |
| 72 | + |
| 73 | + for (int i = 0; i < n; i++) { |
| 74 | + int arr_item = stoi(arr_temp[i]); |
| 75 | + |
| 76 | + arr[i] = arr_item; |
| 77 | + } |
| 78 | + |
| 79 | + int res = minimumSwaps(arr); |
| 80 | + |
| 81 | + fout << res << "\n"; |
| 82 | + |
| 83 | + fout.close(); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +vector<string> split_string(string input_string) { |
| 89 | + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { |
| 90 | + return x == y and x == ' '; |
| 91 | + }); |
| 92 | + |
| 93 | + input_string.erase(new_end, input_string.end()); |
| 94 | + |
| 95 | + while (input_string[input_string.length() - 1] == ' ') { |
| 96 | + input_string.pop_back(); |
| 97 | + } |
| 98 | + |
| 99 | + vector<string> splits; |
| 100 | + char delimiter = ' '; |
| 101 | + |
| 102 | + size_t i = 0; |
| 103 | + size_t pos = input_string.find(delimiter); |
| 104 | + |
| 105 | + while (pos != string::npos) { |
| 106 | + splits.push_back(input_string.substr(i, pos - i)); |
| 107 | + |
| 108 | + i = pos + 1; |
| 109 | + pos = input_string.find(delimiter, i); |
| 110 | + } |
| 111 | + |
| 112 | + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); |
| 113 | + |
| 114 | + return splits; |
| 115 | +} |
0 commit comments