핑구

[백준/JAVA] 10989번 : 수 정렬하기 3 본문

CS/알고리즘

[백준/JAVA] 10989번 : 수 정렬하기 3

코딩 펭귄 2021. 9. 7. 19:45

https://www.acmicpc.net/problem/10989

 

10989번: 수 정렬하기 3

첫째 줄에 수의 개수 N(1 ≤ N ≤ 10,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 10,000보다 작거나 같은 자연수이다.

www.acmicpc.net

 

 

해당 문제에서는 Scanner를 사용하여 입력받는 경우 시간 초과가 발생합니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {
		int[] count = new int[10001];

		try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			int num = Integer.parseInt(br.readLine());
			
			for(int i = 0; i < num; i++) {
				count[Integer.parseInt(br.readLine())]++;
			}
			
			StringBuilder sb = new StringBuilder();
			
			for(int i = 1; i < count.length; i++) {
				while(count[i] > 0) {
					sb.append(i).append('\n');
					count[i]--;
				}
			}
			
			System.out.println(sb);
			
		} catch (NumberFormatException e) {
		} catch (IOException e) {
		}
	}
}