Created
May 2, 2022 07:44
-
-
Save valkiie/09c3e19044cae2fd35cac7638bd8dfc8 to your computer and use it in GitHub Desktop.
Lily's Homework HackerRank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
import java.security.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.function.*; | |
import java.util.regex.*; | |
import java.util.stream.*; | |
import static java.util.stream.Collectors.joining; | |
import static java.util.stream.Collectors.toList; | |
class Result { | |
/* | |
* Complete the 'lilysHomework' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts INTEGER_ARRAY arr as parameter. | |
*/ | |
public static int lilysHomework(List<Integer> arr) { | |
int swapCount = 0; | |
int reverseSwapCount = 0; | |
int n = arr.size(); | |
int[] hw = new int[n]; | |
int[] hw2 = new int[n]; | |
Integer[] hwAscSort = new Integer[n]; | |
Integer[] hwDescSort = new Integer[n]; | |
Map<Integer,Integer> original = new HashMap<>(); | |
Map<Integer,Integer> original2 = new HashMap<>(); | |
//Initialization of variables | |
for(int i = 0; i < n; i++) | |
{ | |
int current = arr.get(i); | |
hwAscSort[i] = current; | |
hwDescSort[i] = current; | |
hw[i] = current; | |
hw2[i] = current; | |
original.put(current,i); | |
original2.put(current,i); | |
} | |
Arrays.sort(hwAscSort); //ascending sort | |
Arrays.sort(hwDescSort, Collections.reverseOrder()); //descending sort | |
//swap the elements from homework to the correct position | |
for(int i = 0; i < n; i++) | |
{ | |
if(hw[i] != hwAscSort[i]) | |
{ | |
int tmp = hw[i]; | |
hw[i] = hw[original.get(hwAscSort[i])]; | |
hw[original.get(hwAscSort[i])] = tmp; | |
original.put(tmp,original.get(hwAscSort[i])); | |
swapCount++; | |
} | |
} | |
//swap the elements from homework to the correct position | |
for(int i = 0; i < n; i++) | |
{ | |
if(hw2[i] != hwDescSort[i]) | |
{ | |
int tmp = hw2[i]; | |
hw2[i] = hw2[original.get(hwDescSort[i])]; | |
hw2[original2.get(hwDescSort[i])] = tmp; | |
original2.put(tmp, original2.get(hwDescSort[i])); | |
reverseSwapCount++; | |
} | |
} | |
return Math.min(swapCount,reverseSwapCount); | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); | |
int n = Integer.parseInt(bufferedReader.readLine().trim()); | |
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()); | |
int result = Result.lilysHomework(arr); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment