본문으로 바로가기

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