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
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的更多相关文章
- 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 ...
- 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 ...
- LeetCode题解 | 215. 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...
- 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 ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- 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 ...
- 【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 ...
- 【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 ...
- Leetcode题目215.数组中的第K个最大元素(中等)
题目描述: 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...
随机推荐
- c# mvc会话过期设置
configuration 元素(常规设置架构) system.web 元素(ASP.NET 设置架构) sessionState 元素(ASP.NET 设置架构) <session ...
- Dev GridView RowCellClick事件与MouseDown事件
GridView处于可编辑状态,左键点击默认为“进入编辑”. 将GridView的OptionsColumn.AllowEdit设置为false后左键可触发RowCellClick.但有时候,既希望G ...
- 《高性能mysql》笔记
组合索引和sql中的顺序有关 单列索引和sql中的顺序无关
- disruptor 问题排查
需求:收到银行异步通知,要在2秒内将结果返回银行,同时还要根据银行返回的交易状态更新数据库订单状态和其他业务. 采用disruptor,其实最好使用独立MQ产品.本次用的是disruptor,遇到了一 ...
- c#之GDI简单实现代码及其实例
作业:文档形式 3到5页理解 1.理解 2.源代码解释(1到2页) 3.实现效果 项目地址: https://github.com/zhiyishou/polyer Demo:https://zhiy ...
- Vue.js-----轻量高效的MVVM框架(六、Class与Style绑定)
这个相对来说简单,看一遍代码就懂. 一.完整片段: <!DOCTYPE html> <html> <head> <meta charset="UTF ...
- vscode写vue模板--代码片段
Ctrl+Shift+P打开命令输入 snippet (打开用户代码片段) 在输入vue(选择代码片段的语言) 如果搜索不到,安装一个插件 vueHelper 如果搜索到复制粘贴以下代码 { &quo ...
- 常见的几种web攻击方式
一.Dos攻击(Denial of Service attack) 是一种针对服务器的能够让服务器呈现静止状态的攻击方式.有时候也加服务停止攻击或拒绝服务攻击.其原理就是发送大量的合法请求到服务器,服 ...
- cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
真实验证有效:点击链接 查看具体移植过程.
- 记一次无法登录 wine QQ
入Linux坑第X天,过了五一小长假,回来布置我的环境,本来不应该装一些不必要的东西分自己心,但还是装上,以便不时之需. 把输入法装好后,就安装了QQ,查过资料,都说wine_QQ国际版可以使用,于是 ...