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 ...
随机推荐
- javascript中的一元操作符
题目如下: var s1 = "01"; var s2 = "1.1"; var s3 = "z"; var b = false; var ...
- SQL注入工具sqlmap的注入过程记录
1.sqlmap的get注入 假设目标是 https://www.baidu.com/news.php?id=1&data=2 sqlmap语句 列库 sqlmap.py -u "h ...
- Could not read settings.xml
这个问题为什么会发生? 其实不要想太多, 1.文件格式是utf-8 2.其中的报文格式非常重要,千万不能弄错,如果多了一处注释,就会发生以上问题,拼写的时候多注意语义
- centos7基本环境搭建
1. 准备权限:让普通用户具备sudo执行权限 切换到root用户,su # vi /etc/sudoers 添加 koushengrui ALL=(ALL) ALL 这里很容易忘 ...
- Mac 终端执行sql命令
https://www.cnblogs.com/sbj-dawn/p/6905930.html. 远程登录MYSQL数据库 > mysql -u username -p -h hostname ...
- Mybatis学习笔记16 - bind标签
1.${}拼串进行模糊查询,不安全 示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import java ...
- 关于KEIL下的图形化显示和输出问题
一 keil自带的虚拟示波器:只能在软件仿真下使用:右边的这个可以查看变量. 二 SEGGER的工具软件: 1 RTT:可以进行日志输出调试信息输出,需要配置工程,但不占用串口. 2 JSCOPE:可 ...
- PlayMaker Action的执行顺序
如图:默认的是从上到下 先执行Play Sound,再执行Destroy Object. 可以点击右上角的齿轮,也就是设置按钮选中Action Sequence,这样就会同时执行.
- JEECMS站群管理系统-- Jeecms安装过程
Jeecms是基于java技术研发的站群管理系统,稳定.安全.高效.跨平台.无限扩展是jeecms 的优点,系统支持mysql.oracle.sqlserver.db2等主流数据库. 轻松建设大规模网 ...
- mathjax符号
mathjax公式 \(\delta\): \delta \(\Delta\): \Delta \(\int\): \int \(\iint\): \iint \(\approx\): \approx ...