99클럽 코테 스터디 32일차 TIL - h index

수삼·2024년 11월 28일
0

코딩테스트

목록 보기
37/44

import java.io.*;
import java.util.Collections;
import java.util.PriorityQueue;

class Solution {
    public int solution(int[] citations) {
        
        PriorityQueue<Integer> pq = new PriorityQueue(Collections.reverseOrder());
        
        for (Integer citation : citations) {
            pq.add(citation);
        }

        int hIndex = 0;
        while (!pq.isEmpty()) {
            if (pq.peek() > hIndex) {
                pq.poll();
                hIndex++;
            } else {
                break;
            }
        }
        return hIndex;
    }
}
  1. 큐에 넣는다
  2. 가장 앞에 있는 값이 꺼낸 값보다 클때까지만 계속 뽑아준다

0개의 댓글