Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
find Kth largest element in array
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { System.out.println("Hello, World!"); System.out.println(findKthLargest(new int[]{3,2,1,5,6,4},2)); } public static int findKthLargest(int[] nums, int k) { // init heap 'the smallest element first' PriorityQueue<Integer> heap = new PriorityQueue<Integer>(); // keep k largest elements in the heap for (int n: nums) { heap.add(n); if (heap.size() > k) heap.poll(); } // output return heap.poll(); } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Frequency
MinMaxArray
1 to 100 except 30 to40
Bbs
1.6
Size and signedness of Java ints and longs
Java - SimpleDateFormat(ting)
Coding Challenge - 02 (Odd numbers)
moneda
Write a program to sort the odd elements descending order and even elements in ascending order
Please log in to post a comment.