본문 바로가기
Problem Solving/LeetCode

[LeetCode] 703. Kth Largest Element in a Stream - Java

by graycode 2026. 9. 28.

• 문제 링크

 

Kth Largest Element in a Stream - LeetCode

Can you solve this real interview question? Kth Largest Element in a Stream - You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interv

leetcode.com

 

• 풀이 코드

import java.util.PriorityQueue;

public class KthLargest {

    private final PriorityQueue<Integer> pq;
    private final int k;

    public KthLargest(int k, int[] nums) {
        this.k = k;
        this.pq = new PriorityQueue<>(k);

        for (int num : nums) add(num);
    }

    public int add(int val) {
        pq.offer(val);
        if (pq.size() > k) pq.poll();

        return pq.element();
    }

}

댓글