N번째 큰 수 - 2075

Seongjin Jo·2023년 6월 9일
0

Baekjoon

목록 보기
39/51

문제

풀이

import java.util.*;

// n번째 큰 수 - S2 - 정렬
public class ex2075 {
    static int n;

    static PriorityQueue<Integer> q = new PriorityQueue<>(Collections.reverseOrder());
    public static void solution(){
        for(int i=0; i<n-1; i++){
            q.poll();
        }

        System.out.println(q.poll());
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                q.offer(sc.nextInt());
            }
        }
        solution();
    }
}

처음에 그냥 무지성으로 풀었는데 시간초과. 그래서 우선순위큐로 풀었다. 역순으로 우선순위큐를 선언하면 큰수부터 나오는 자료구조이다. 그렇게 해서 답 이전까지 다 poll해주고 답도 poll해서 출력해주면되는 문제이다!!

0개의 댓글