algorithm/문제 풀이

백준 10989번 (수 정렬하기 3) Java 11 계수 정렬

ssoheeh 2021. 2. 8. 11:41

문졔 : 수 정렬하기 3

시간 제한 : 3 sec

메모리 제한 : 8MB

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.*;
import java.util.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));    
        StringTokenizer st;
        int[] count = new int[10001];
        int n = Integer.parseInt(br.readLine());
        Arrays.fill(count, 0);
        for (int i = 0; i < n; i++) {
            count[Integer.parseInt(br.readLine())]++;
        }
        for (int i = 1; i <= 10000; i++) {
            if(count[i]!=0)
                for (int j = 0; j < count[i]; j++) {
                    bw.write(Integer.toString(i));
                    bw.newLine();
                }
        }
        bw.flush();
        br.close();
        bw.close();
    }
}
cs

정렬해야하는 수 값의 범위가 작다면 계수 정렬 고려해볼 것!

시간 복잡도가 O(N)이다.