From 26a8ac14130cb1f358c1753f9638d074ea62fc86 Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Sun, 31 Mar 2019 17:55:43 +0200 Subject: [PATCH 1/2] Create min_swaps1.cpp --- hackerrank/array/min_swaps1.cpp | 115 ++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 hackerrank/array/min_swaps1.cpp diff --git a/hackerrank/array/min_swaps1.cpp b/hackerrank/array/min_swaps1.cpp new file mode 100644 index 0000000..d048d96 --- /dev/null +++ b/hackerrank/array/min_swaps1.cpp @@ -0,0 +1,115 @@ + +// Original Link +// https://www.hackerrank.com/challenges/minimum-swaps-2/problem + +#include + +using namespace std; + +vector split_string(string); + + + + +///////////////// SOLUTION ////////////////// + +// Idea is to solve it with a recurrent function which iteratively performs greedy swaps + +void swap(vector& v, const unsigned int a, const unsigned int b) +{ + int temp; + temp = v[a]; + v[a] = v[b]; + v[b] = temp; +} + +unsigned int search(const vector& v, const unsigned int p, const int q) +{ + for(unsigned int i=p; i& v, const unsigned int i, const unsigned int cont) +{ + // Recursion Ending Conditions + if(i == v.size()-1) return cont; + + // Expected Number + const int n = i+1; + if(v[i] == n) return solve(v, i+1, cont); + else + { + const unsigned int j = search(v, i+1, n); + swap(v, i, j); + return solve(v, i+1, cont+1); + } +} + +// Complete the minimumSwaps function below. +int minimumSwaps(vector arr) { + return solve(arr, 0, 0); +} + + + + +///////////////////////////////////////// + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + int n; + cin >> n; + cin.ignore(numeric_limits::max(), '\n'); + + string arr_temp_temp; + getline(cin, arr_temp_temp); + + vector arr_temp = split_string(arr_temp_temp); + + vector arr(n); + + for (int i = 0; i < n; i++) { + int arr_item = stoi(arr_temp[i]); + + arr[i] = arr_item; + } + + int res = minimumSwaps(arr); + + fout << res << "\n"; + + fout.close(); + + return 0; +} + +vector split_string(string input_string) { + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { + return x == y and x == ' '; + }); + + input_string.erase(new_end, input_string.end()); + + while (input_string[input_string.length() - 1] == ' ') { + input_string.pop_back(); + } + + vector splits; + char delimiter = ' '; + + size_t i = 0; + size_t pos = input_string.find(delimiter); + + while (pos != string::npos) { + splits.push_back(input_string.substr(i, pos - i)); + + i = pos + 1; + pos = input_string.find(delimiter, i); + } + + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); + + return splits; +} From 2f7f5fb52c8a54383aa7b9c4d2a458b409319048 Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Sun, 31 Mar 2019 18:31:03 +0200 Subject: [PATCH 2/2] Create readme.md --- hackerrank/array/readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 hackerrank/array/readme.md diff --git a/hackerrank/array/readme.md b/hackerrank/array/readme.md new file mode 100644 index 0000000..b8b535d --- /dev/null +++ b/hackerrank/array/readme.md @@ -0,0 +1,11 @@ + +# Overview + +Some Hackerrank Related Exercises + +Minimum Swaps 2 +- [Def](https://www.hackerrank.com/challenges/minimum-swaps-2/problem) +- [Sol](min_swaps1.cpp) +- Hint + - Solved used recursion and in place swap +