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 ...
随机推荐
- 10.Spring集成一
1.Email 电子邮件工作原理: 邮件发送方通过邮件发送客户端把邮件发送到发送方的邮件服务器,在发送的过程中需要用到SMTP协议发送方的邮件服务器把邮件发送到接收方的邮件服务器,使用的协议也是SMT ...
- 转——深度学习之BN算法(Batch Normailization)
Batch Normalization 学习笔记 原文地址:http://blog.csdn.net/hjimce/article/details/50866313 作者:hjimce 一.背景意义 ...
- js生成随机码(只含有数字和字母的随机码)
/*** randomWord 产生任意长度随机字母数字组合** randomFlag 是否任意长度 min 任意长度最小位[固定位数] max 任意长度最大位*/ function randomWo ...
- 自动化构建工具maven
Maven是目前最流行的自动化构建工具,对于生产环境下多框架.多模块整合开发有重要作用.Maven 是一款在大型项目开发过程中不可或缺的重要工具. 一.什么是构建? 构建并不是创建,创建一个工程并不等 ...
- MATLAB矩阵求值和稀疏矩阵
方阵的行列式: det(A) 矩阵线性无关的行数或列数,称为矩阵的秩. rank(A) 求3~20阶魔方矩阵的秩 for n=3:20 rank(magic(n)) end 矩阵的迹等于矩阵的对角线元 ...
- 硬盘和显卡的访问与控制(一)——《x86汇编语言:从实模式到保护模式》读书笔记01
本文是<x86汇编语言:从实模式到保护模式>(电子工业出版社)的读书实验笔记. 这篇文章我们先不分析代码,而是说一下在Bochs环境下如何看到实验结果. 需要的源码文件 第一个文件是加载程 ...
- 封装RateLimiter 令牌桶算法
自定义注解封装RateLimiter.实例: @RequestMapping("/myOrder") @ExtRateLimiter(value = 10.0, timeOut = ...
- 如何实现Nginx+Keepalived中Nginx进程的高可用
此架构我简单说明下: 一般为了维护方便,企业网站的服务器都在自己的内部机房里,只开放了Keepalived的VIP地址的两个端口80.443,通过Juniper SSG550防火墙映射出去,外网DNS ...
- 打乱式排序的Java版实现
项目中涉及到对大批量的数据进行打乱式排序,大概原理如下: 输入源数据:1,1,2,3,3,3,4,4 输出结果: 1,2,3,4,1,3,4,3 实现代码如下,采用递归的思想: static &l ...
- NodeJS 开发应用
NodeJS 开发应用 使用的 Node 版本: V8.11.4 开发工具: VSCode 1.27.1 系统: Deepin 15.7 Desktop x64 项目结构 项目结构 Project i ...