Skip to content

Instantly share code, notes, and snippets.

@wohhie
Created July 8, 2021 17:17
Show Gist options
  • Select an option

  • Save wohhie/14d235b2d3e2272c394c8e43ee4fbb17 to your computer and use it in GitHub Desktop.

Select an option

Save wohhie/14d235b2d3e2272c394c8e43ee4fbb17 to your computer and use it in GitHub Desktop.
public int findKthLargestNumber(int[] arr, int k) {
Queue<Integer> queue = new PriorityQueue<>((n1, n2) -> n1.compareTo(n2));
for(int n : arr){
queue.add(n);
if (queue.size() > k){
queue.poll();
}
}
return queue.poll();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment