In this post, we will solve Lily’s Homework HackerRank Solution. This problem (Lily’s Homework) is a part of HackerRank Problem Solving series.
Solution – Lily’s Homework – HackerRank Solution
C++
#include <bits/stdc++.h> using namespace std; vector <string> split_string(string); // Complete the lilysHomework function below. int lilysHomework(vector<int> arr_orig) { int result = INT_MAX; vector<int> sorted(arr_orig); sort(sorted.begin(), sorted.end(), greater<int>()); for (int rev = 0; rev < 2; rev++) { int curSwap = 0; if (rev) { reverse(sorted.begin(), sorted.end()); } vector<int> arr(arr_orig); // val, pos unordered_map<int, int> val2pos; for (int i = 0; i < arr.size(); i++) { val2pos[arr[i]] = i; } for (int i = 0; i < arr.size(); i++) { if (arr[i] == sorted[i]) { continue; } int ai = arr[i]; int si = sorted[i]; swap(arr[i], arr[val2pos[si]]); curSwap++; val2pos[ai] = val2pos[si]; val2pos[si] = i; } result = min(result, curSwap); } return result; } int main() { ofstream fout(getenv("OUTPUT_PATH")); int n; cin >> n; cin.ignore(numeric_limits<streamsize>::max(), '\n'); string arr_temp_temp; getline(cin, arr_temp_temp); vector <string> arr_temp = split_string(arr_temp_temp); vector<int> arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = lilysHomework(arr); fout << result << "\n"; fout.close(); return 0; } vector <string> 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 <string> 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; }
Python
import math import os import random import re import sys # # Complete the 'lilysHomework' function below. # # The function is expected to return an INTEGER. # The function accepts INTEGER_ARRAY arr as parameter. # n = int(input()) l = list(map(int, input().split())) sort = sorted(l) rev = list(reversed(l)) d = {} for i in range(n): if sort[i] not in d: d[sort[i]] = i swaps = 0 i = 0 while i < n: if sort[i] == l[i]: i += 1 continue swaps += 1 l[d[l[i]]], l[i] = l[i], l[d[l[i]]] d[sort[i]] += 1 d = {} for i in range(n): if sort[i] not in d: d[sort[i]] = i swaps_rev = 0 i = 0 while i < n: if sort[i] == rev[i]: i += 1 continue swaps_rev += 1 rev[d[rev[i]]], rev[i] = rev[i], rev[d[rev[i]]] d[sort[i]] += 1 print(min(swaps, swaps_rev))
Java
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int sortedSwaps = 0; int[] homework = new int[n]; Integer[] homeworkSorted = new Integer[n]; Map<Integer,Integer> original = new HashMap<>(); int sortedReverseSwaps = 0; int[] homework2ndCopy = new int[n]; Map<Integer,Integer> original2ndCopy = new HashMap<>(); //Initialize our arrays and maps for(int i = 0; i < n; i++) { homeworkSorted[i] = input.nextInt(); homework[i] = homeworkSorted[i]; homework2ndCopy[i] = homeworkSorted[i]; original.put(homework[i],i); original2ndCopy.put(homework2ndCopy[i],i); } Arrays.sort(homeworkSorted);//Sort the input ascending for(int i = 0; i < n; i++) { if(homework[i] != homeworkSorted[i]) { //swap the element from homework to the right position int tmp = homework[i]; homework[i] = homework[original.get(homeworkSorted[i])]; homework[original.get(homeworkSorted[i])] = tmp; //Update index after swap original.put(tmp,original.get(homeworkSorted[i])); sortedSwaps++; } } Arrays.sort(homeworkSorted, Collections.reverseOrder());//Sort the input descending for(int i = 0; i < n; i++) { if(homework2ndCopy[i] != homeworkSorted[i]) { //swap the element from homework to the right position int tmp = homework2ndCopy[i]; homework2ndCopy[i] = homework2ndCopy[original.get(homeworkSorted[i])]; homework2ndCopy[original2ndCopy.get(homeworkSorted[i])] = tmp; //Update index after swap original2ndCopy.put(tmp, original2ndCopy.get(homeworkSorted[i])); sortedReverseSwaps++; } } System.out.println(Math.min(sortedSwaps,sortedReverseSwaps));//Choose the smallest of the two possible smallest } }
Note: This problem (Lily’s Homework) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.