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

  PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);

using lamba function which implemented the Comparator

-------------------------------------------------------------------------------------------------------

update other solution

Leetcode back(215) to be continue的更多相关文章

  1. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

  2. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

  3. LeetCode题解 | 215. 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

  4. LeetCode(215) Kth Largest Element in an Array

    题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  5. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

  6. LeetCode OJ 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  8. 【leetcode】215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. Leetcode题目215.数组中的第K个最大元素(中等)

    题目描述: 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...

随机推荐

  1. jquery知识点结合使用

    1.在页面的某个具有id的element内插入其他element,并给其增加悬浮提示. $('#id').after('<div id='box'></div>'); $('# ...

  2. HDU1085 Holding Bin-Laden Captive!

    Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long t ...

  3. python3 练习3

    ##c##写法 #include<iostream>using namespace std;class Rectangle{public:    int j;void area(int X ...

  4. git知识点总结

    集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器.集中式版本控制系统最大的 ...

  5. C# 空字典遍历

    Data.instance.boolMap 是一个字典. 若Data.instance.boolMap == null 的话,遍历的时候,就会报错: 若Data.instance.boolMap = ...

  6. PlayMaker Get Parent 拿到父物体

    这里是拿到自己的父物体,然后存储到Parent这个GameObject变量里. 然后在Parent那个位置生成一个Coin,也就是在父物体那生成一个Coin.

  7. (转)轻松应对IDC机房带宽突然暴涨问题

    原文:http://blog.51cto.com/oldboy/909696

  8. 注册中心eureka

    最近在忙一些其它的事情,两个城市来回跑还要办一些手续,挺费劲的,学习的事情也就耽误了一些,尽量赶吧. spring cloud为分布式的微服务架构提供了一站式的解决方案,eureka注册中心在spri ...

  9. ajax的serialize()方法

    自己看吧,超级简单,就不用挨个获取表单名称和值对装在Json里往php传了,直接传个form就可以. [HTML] <form method="post" id=" ...

  10. URAL 1142——Relations——————【dp】

    A - Relations Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...