코딩테스트/알고리즘
해커랭크 Hackerrank ) Migratory Birds
디깅
2023. 5. 27. 16:33
728x90
https://www.hackerrank.com/challenges/migratory-birds/problem?isFullScreen=true
Migratory Birds | HackerRank
Determine which type of bird in a flock occurs at the highest frequency.
www.hackerrank.com
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 'migratoryBirds' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static int migratoryBirds(List<Integer> arr) {
Map<Integer,Integer> mapList = new HashMap<Integer,Integer>() {{
put(1,0);
put(2,0);
put(3,0);
put(4,0);
put(5,0);
}};
for (int i = 0; i < arr.size(); i++) {
mapList.put(arr.get(i),mapList.get(arr.get(i)+1));
}
int countList[] = new int[5];
for (int i : countList) {
countList[i] = mapList.get(i);
}
Arrays.sort(countList);
return countList[0];
}
}
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 arrCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.migratoryBirds(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
반응형