Skip to content

고급정렬 알고리즘 - 수 정렬하기 2 (백준 2751번) Java 코드 제출시 시간 초과 발생합니다. #5

Description

@vel1024

출력시에 System.out.println(x) 로 하나씩 출력하는게 아니라
StringBuilder 를 사용해서 한번에 출력하면 시간초과를 피할 수 있습니다.

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> data = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(br.readLine());
            data.add(x);
        }

        Collections.sort(data);

        StringBuilder sb = new StringBuilder();

        for (int x: data) {
            sb.append(x).append("\n");
        }

        System.out.println(sb);

        br.close();
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions