My simple solution using PriorityQueue(JAVA)


  • 0
    H

    '''
    public class Solution {

    public int findKthLargest(int[] a, int k) {
    PriorityQueue<Integer> q = new PriorityQueue<>(k, new Comparator<Integer>(){
    @Override
    public int compare(Integer a, Integer b){
    return (int)a-b;
    }
    });
    int i = 0;
    for(; i< a.length; i++){
    q.offer(a[i]);
    if(q.size() > k) q.poll();
    }
    return (int)q.peek();

    }

    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.