본문 바로가기

알고리즘

백준 2075번 N번째 큰 수 자바

728x90
반응형

❓문제

N×N의 표에 수 N2개 채워져 있다. 채워진 수에는 한 가지 특징이 있는데, 모든 수는 자신의 한 칸 위에 있는 수보다 크다는 것이다. N=5일 때의 예를 보자.

12 7 9 15 5
13 8 11 19 6
21 10 26 31 16
48 14 28 35 25
52 20 32 41 49

이러한 표가 주어졌을 때, N번째 큰 수를 찾는 프로그램을 작성하시오. 표에 채워진 수는 모두 다르다.


🔠 입력

첫째 줄에 N(1 ≤ N ≤ 1,500)이 주어진다. 다음 N개의 줄에는 각 줄마다 N개의 수가 주어진다. 표에 적힌 수는 -10억보다 크거나 같고, 10억보다 작거나 같은 정수이다.


🖨️출력

첫째 줄에 N번째 큰 수를 출력한다.


✍️ 예제 입력 

5
12 7 9 15 5
13 8 11 19 6
21 10 26 31 16
48 14 28 35 25
52 20 32 41 49

✔️ 예제 출력

35

💡해설

N번째 큰 수를 출력하는 법은 PriorityQueue를 이용해서 정렬한 후 출력하면 된다.

큰 수를 출력해야 하기 때문에 reverseOrder를 사용한다. 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
        int[][] arr = new int[N][N];
        for(int i = 0; i < N; i++) {
            String[] line = br.readLine().split(" ");
            for(int j = 0; j < N; j++) {
                arr[i][j] = Integer.parseInt(line[j]);
                queue.add(arr[i][j]);
            }
        }


        for(int i = 1; i <= N; i++) {
            if(i == N) {
                System.out.println(queue.poll());
                break;
            }
            queue.poll();
        }

        br.close();
    }
}
728x90
반응형