LeetCode 215. Kth Largest Element in an Array--数字第K大的元素--最大堆或优先队列--C++,Python解法
题目地址:Kth Largest Element in an Array - LeetCode
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.
这道题目是经典的求数组第K大元素的题目。
最坏的解法是先排序,再找。时间复杂度是 O(NlogN)。
好一些的办法是使用最大堆进行排序,时间复杂度是O(NlogK)。
另外一种做法是使用Quickselect
,但是最坏的时间复杂度跟快排一样是N^2,我就不写了。
Python解法如下:
import heapq
class Solution:def findKthLargest(self, nums: List[int], k: int) -> int:return heapq.nlargest(k, nums)[-1]
C++解法如下:
#include <vector>
#include <queue>
using namespace std;class Solution {public:int findKthLargest(vector<int>& nums, int k) {priority_queue<int,vector<int>,greater<int>> q;for(int i=0;i<nums.size();i++){if(q.size()<k) q.push(nums[i]);else if(q.top()<nums[i]){q.pop();q.push(nums[i]);}}return q.top();}
};
LeetCode 215. Kth Largest Element in an Array--数字第K大的元素--最大堆或优先队列--C++,Python解法相关推荐
- 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array...
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- [swift] 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 Kth Largest Element in an Array
思路: 跟找第kth小的数相反,数组逆向排序.从前找第k - 1个数. 1 public class Solution { 2 public int findKthLargest(int[] arra ...
- leetcode 215. Kth Largest Element in an Array | 215. 数组中的第K个最大元素(Java)
题目 https://leetcode.com/problems/kth-largest-element-in-an-array/ 题解 本题需要建立大顶堆.关于堆的数据结构,需要知道: 堆 什么是堆 ...
- [leedcode 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 ...
- 215. Kth Largest Element in an Array 数组中的第K个最大元素
Title 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...
- leetcode之Kth Largest Element in an Array
这题是求出来排序好的数的第k个大的数.用了.sort()之后就非常简单了.不用自己排序的表示真是简单啊.然而真讲排序我还基本不会...代码如下: class Solution(object):def ...
- 力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)
题目链接:https://leetcode.cn/problems/kth-largest-element-in-an-array 目录: 一.题目描述 1.中文 2.英文 二.解决方法 1.直接排序 ...
最新文章
- r - 求平均成绩_学霸秘籍:小学数学知识点例题讲解 — 平均数问题
- Spark详解(十一):Spark运行架构原理分析
- linux设置进程优先级,Linux线程属性及优先级设置
- 八皇后java_经典八皇后问题:Java语言
- 2019小程序没必要做了_企业有必要开发微信小程序吗?
- 减少Java垃圾收集开销的5条提示
- 《掌握需求过程》阅读笔记05
- 14 对于移动类型561和账户xxxxxx Ext. GA本币计的金额(023)的不同的字段
- 4.3.1 jQuery基础(2)
- 腐蚀rust高速箭怎么做不了_高速公路波形护栏板安装时的设置要求
- 大数据之-Hadoop3.x_MapReduce_ReduceTask源码解析---大数据之hadoop3.x工作笔记0127
- Linux安装python环境方法
- [Jscript]Js导出Excel
- 软媒魔方5.13发布:制作U盘启动,更快速
- matlab数字图像处理常用操作
- (一)vmware中Linux共享文件夹设置
- form表单Get方式提交时,action中带参数传递不了
- 使用GOOGLE API做了个简繁英互译小工具
- 01背包问题 动态规划求解方法 动态方程的详细解释 能理解的解释(附python代码)
- Ubuntu中install.sh文件如何执行
热门文章
- OpenCV | OpenCV彩色图像直方图算法实现
- 零基础入门学习Python(5)Python的数据类型
- BFS:南土所梁玉婷组综述促进土壤植物生态系统健康的微生物共存理论
- 【首轮官宣】中国肠道大会姊妹盛会,GUT 2022正式启航!
- NC:中国药科郝海平和郑啸发现饮食-微生物互作缓解肠损伤
- 蚊子已经很可怕了,而这些吸血昆虫能让你感受真正的恐惧
- 宏基因组学习交流5群成立
- Annual Review:人与微生物组
- R语言ggplot2可视化:使用堆叠的条形图(Stacked Barplot)可视化每个数据行(row)的缺失值的情况(Visualizing missing data counts in rows)
- pandas生成新的累加数据列、pandas生成新的累加数据列(数据列中包含NaN的情况)、pandas计算整个dataframe的所有数据列的累加