[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_03/id_36/LeetCode_703_036.java [Back]  [Original]

package com.potato.leetcode.sort;

import java.util.PriorityQueue;

public class LeetCode_703_036 {
    final PriorityQueue q;
    final int k;

    public LeetCode_703_036(int k, int[] nums) {
        this.k = k;
        q = new PriorityQueue(k);
        for (int i : nums) {
            add(i);
        }
    }

    public int add(int n) {
        // K
        if (q.size() < k) {
            q.offer(n);
        } else if (q.peek() < n) {
            q.poll();
            q.offer(n);
        }
        return q.peek();
    }
}

Web Proxy Viewer  |  New URL  |  Original Page