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 ...
随机推荐
- 江西财经大学第一届程序设计竞赛 D
链接:https://www.nowcoder.com/acm/contest/115/D来源:牛客网 题目描述 事情,是这样的. 有这么一天双休日的中午. 我刚把我衣服扔进了洗衣机,然后拿了个小板凳 ...
- lnmp 架构
Mysql安装 tar zxf mysql-boost-5.7.17.tar.gz yum install -y gcc gcc-c++ yum install cmake-2.8.12.2-4.el ...
- log4net独立配置文件配置(winfrom)
log4net配置很多,具体配置步骤不细说,具体说出个人遇到的问题. 在winfrom和web应用程序中配置,在默认配置文件配置都没问题,因为EF也写在默认配置文件中,就会冲突解决办法就是将log4. ...
- ymPrompt.js消息提示组件
转载:https://www.cnblogs.com/linzheng/archive/2010/11/15/1878058.html 使用说明: 1.在页面中引入ymPrompt.js.如:< ...
- 安装tomcat时遇到的问题
1.刚开始在eclipse配置的tomcat是免安装的,后来提示 所以后来配置了一个安装版的. 2.后来运行server发现报错:8080,8005,端口被占用,然后关闭xammp上的server,然 ...
- oracle 12.1.0.2的mgmt 导致的ORA-01017 bug
两节点12c RAC,在两节点上export ORACLE_SID再sqlplus / as sysdba都正常登录,然而Commvault通过service_name方式(sqlplus sys/p ...
- 德国生活tips
提要: 在德国生活也近7个月的时间了,简单给准备来德国留学,生活或者是旅游的人写一些小tips.想到什么就写什么咯. (1)德国交通篇 在德国,交通是第一要点,一般大家都会看到城市里有Straßenb ...
- MySQL三大范式和反范式
1. 第一范式确保数据表中每列(字段)的原子性.如果数据表中每个字段都是不可再分的最小数据单元,则满足第一范式.例如:user用户表,包含字段id,username,password 2. 第二范式在 ...
- Robot Framework搭建
需要安装的内容如下: 1. Python2.7.13(听说python3对RF支持的不是很好,所以我下的Python2) 2. wxPython 2.8.12.1(只能这个版本) 3. robotfr ...
- Murano Weekly Meeting 2015.07.28
Meeting time: 2015.July.28th 1:00~2:00 Chairperson: Kirill Zaitsev, core from Mirantis Meeting summa ...