
Description
각 요소의 개수를 기록하여 정렬하는 counting sort를 구현하는 문제입니다.
Solution 1. HashTable + Stream API
public static List<Integer> countingSort(List<Integer> arr) {
int[] map = new int[100];
for(int i: arr){
map[i]++;
}
return Arrays.stream(map).boxed().collect(Collectors.toList());
}
hash table 로 사용할 인덱스를 key로 하는 배열을 선언하여 각 요소의 숫자를 기록해 준뒤 stream api를 이용해 List<Integer>로 변환하여 반환해 줍니다.
Reference
Counting Sort 1 | HackerRank
Count the number of times each value appears.
www.hackerrank.com
'알고리즘 > HackerRank' 카테고리의 다른 글
[HackerLank] Zig Zag Sequence - 문제풀이 (0) | 2022.03.15 |
---|---|
[HackerLank] Flipping the Matrix - 문제풀이 (0) | 2022.03.15 |
[HackerLangk] Diagonal Difference - 문제풀이 (0) | 2022.03.15 |
[HackerLank] Lonely Integer - 문제풀이 (0) | 2022.03.15 |
[HackerLank] Find the Median - 문제풀이 (0) | 2022.03.13 |