Leetcode back(215) to be continue
solution discussion
https://leetcode.com/problems/kth-largest-element-in-an-array/description/ -- 215 problems
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained
create the min heap first in java
https://www.geeksforgeeks.org/priority-queue-class-in-java-2/ -- reference of heap in java
PriorityQueue<Integer> queue = new PriorityQueue<>();//min heap
offer the element from the nums to the min heap
poll the k largest element
class Solution { public int findKthLargest(int[] nums, int k) { //sorting the arrya and find the second largest //or heap /*PriorityQueue<Integer> queue = new PriorityQueue<>( new Comparator(){ //@Override public int compare(Integer num1, Integer num2){ return (num2 - num1); } });*/ PriorityQueue<Integer> queue = new PriorityQueue<>();//min heap //offer the element to the heap for(int i : nums){ queue.offer(i); } //poll the lement fromt he heap for(int i = 1; i<=nums.length - k; i++){ queue.poll(); } return queue.poll(); } }
Question:
how to make max heap in java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
using lamba function which implemented the Comparator
-------------------------------------------------------------------------------------------------------
update other solution