Leetcode215: Kth Largest Element in an Array

第2章: MMU Notifier 全局通知器的核心原理⭐ 摘要 MMU Notifier机制是Linux内核中用于处理内存地址空间变化通知的关键框架。其设计遵循解耦与标准化、最小侵入性、高性能和安全性四大原则,采用三层架构实现上层应用、核心层与内存管理层的协作。框架提供两种通知模型:全局订阅的mmu_notifier和区间订阅的mmu_interval_notifier,分别适用于KVM虚拟化和GPU共享虚拟内存等不同场景。核心数据结构mmu_notifier_ops定义了设备驱动需要实现的回调函数集合,包括进程退出处理、页面老化管理、地址范围失效通知等功能。 阅读详情

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 sorted order, not the kth distinct element.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

题意:

找出一个数组中第K大的元素。

解答①:

这一题和之前做过的一个找出第K小的元素的题目相似哦,从那一题中我学到了用优先队列来做,所以看到这一题我第一个想法就是利用优先队列。同时,利用优先队列找出第K大的元素比找出第K小的元素更加简便。

本题只需要遍历一遍数组,将元素全部添加到队列中,这样的话来建立一个堆。然后根据其性质,将前K-1个元素全部弹出,剩下的处于队列最前面的元素就是需要寻找的第K大的元素。

代码:[cpp] view plain

  1. class Solution {  
  2. public:  
  3.     int findKthLargest(vector<int>& nums, int k) {  
  4.         priority_queue<int,vector<int> >q;  
  5.         for(int i=0;i<nums.size();i++)  
  6.         q.emplace(nums[i]);  
  7.         for(int j=0;j<k-1;j++) q.pop();  
  8.         return q.top();  
  9.     }  
  10. };  
复杂度:

建立堆的时间复杂度是O(n),然后pop的时间复杂度是O(logn),所以总的时间复杂度是O(n)。

解答②:

因为这道题目有一个tag是分治算法,而且这道题有一个典型的解法是利用快排的思想。

选中数组中的一个值作为pivot,然后将小于pivot的数字放在pivot的右边,将大于等于pivot的数字放在其左边。接着判断两边数字的数量,如果左边的个数小于K个,说明第K大的数字在pivot以及pivot右边的区域里面,接着对pivot的右边进行该利用快排思想的操作。要是右边的数量小于K个,说明第K大的数字在pivot以及pivot左边的区域里面,这时对pivot左边执行快排思想的操作。一直到左区域刚好有K-1个数,那么此时的pivot就是第K大的数。
代码:
[cpp] view plain

  1. class Solution {  
  2. public:  
  3.     int findKthLargest(vector<int>& nums, int k) {  
  4.         int high=nums.size();  
  5.         int low=0;  
  6.         while(low<high){  
  7.             int x=low,y=high-1;  
  8.             int pivot=nums[low];  
  9.             while(x<=y){  
  10.                 while(x<=y && nums[x]>=pivot) x++;  
  11.                 while(x<=y && nums[y]<pivot)  y--;  
  12.                 if(x<y) swap(nums[x++],nums[y--]);  
  13.             }  
  14.             swap(nums[low],nums[y]);  
  15.             if(y==k-1) return nums[y];  
  16.             else if(y<k-1) low=y+1;  
  17.             else high=y;  
  18.         }  
  19.     }  
  20. };  
复杂度:

利用快速排序的思想求第K大的数字的时间复杂度是O(n)。

【Unity实战篇】 接入百度翻译,实现文本自动翻译功能 日常在做项目的过程中,游戏本地化几乎已经成为必不可少的一步。 本篇文章将演示怎样在Unity中接入百度翻译,实现文本自动翻译的功能。 阅读详情

相关推荐

李沐之数值稳定性+模型初始化和激活函数

层记为t,这里的y不是预测,还包括了损失函数,y是优化函数,不是预测的结果y。第t层是输出。所有的h都是一些向量,向量关于向量的导数是一个矩阵,所以用了很多次的矩阵乘法,出问题就在这里。

m0_51133942的博客 1530

LeetCode215: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 sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.Note:

2583

FTP新手必看:vsftpd 530登录错误的8种实战解决方案(附详细配置步骤)

本文针对FTP服务配置中常见的vsftpd 530登录错误,提供了8种实战解决方案。从理解错误本质入手,系统性地排查用户列表策略、PAM认证、系统黑名单、Shell限制、目录权限及DNS解析等问题,并附有详细的配置步骤与命令,帮助新手快速定位并解决登录失败难题。

ik678901的博客 700

leetcode 215: Kth Largest Element in an Array

leetcode 215: Kth Largest Element in an Array

西施豆腐渣 1万+

【技术解析】AlexNet——从ReLU到Dropout的深度学习关键突破

本文深入解析AlexNet在深度学习领域的关键突破,重点探讨了ReLU激活函数和Dropout技术的革命性应用。作为卷积神经网络的里程碑,AlexNet通过ReLU解决梯度消失问题,利用Dropout有效防止过拟合,为现代深度学习模型奠定了重要基础。这些创新技术至今仍在计算机视觉和自然语言处理等领域发挥重要作用。

weixin_33719619的博客 282

05-SA8155 QNX Display框架及代码分析(1)

显示是非常重要的一个模块,人机交互的桥梁。那么SA8155 Hypervisor显示是如何处理的呢。对处理流程非常的好奇O(∩_∩)O哈哈~。TODO。

静虚待令,有物混成 1万+

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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5.

哈哈的个人专栏 3873

蓝桥杯 stm32 PWM 测量频率

参考 数据手册:比赛 的板子 上面有两个信号发生器,如下图。通过 扭动 可变电阻 (R40, R39), 来实现 PWM 频率的改变。PA15PB4。PA15,PB4分别使用TIM2_CH1TIM3_CH1来对频率进行捕获。

wuyiyu_的博客 2934

力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

xxxnnn1991的博客 461

即插即用系列(代码实践) | CVPR 2025 FDConv:频域动态卷积,打破密集预测任务的参数效率瓶颈

本文提取自 CVPR 2025 的最新底层算子革新论文《Frequency Dynamic Convolution for Dense Image Prediction》。近年来,动态卷积(Dynamic Convolution)通过融合多个并行卷积核极大地提升了模型性能,但研究表明:这些并行卷积核在频域上的响应高度相似,导致了极其严重的参数浪费,且缺乏对不同频率特征的自适应能力。本文复现了该网络的核心架构——FDConv(频率动态卷积)。该方案创造性地在傅里叶域(Fourier Domain)中学习固定参

分享顶会顶刊论文即插即用模块 123

从零到一破解Uber实时行程API:逆向工程与高并发爬虫实战

在数据采集领域,Uber的实时行程数据始终处于“传说级”难度。动态令牌系统- 每30秒轮换的Bearer Token证书固定(Certificate Pinning)- 阻止中间人攻击请求签名算法- 基于时间戳+用户设备的HMAC-SHA256行为分析- 鼠标轨迹、请求间隔的机器学习模型全链路加密- GraphQL端点的payload加密目录前言:为什么Uber爬虫被称为“地狱难度”?第一章:环境准备与法律边界1.1 技术栈选择(2026年最新)1.2 法律免责声明。

2201_76125261的博客 897

Leetcode 215: Find 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 sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: ...

小猪爱拱地 308

Python爬虫实战:全方位解析前程无忧(51job)职位详情爬取及反爬破解

平台规模:覆盖全国各行业、数百万条岗位信息。数据特点:职位描述详细,职位标签丰富。数据价值:支持招聘趋势分析、职业发展研究、薪资行情调研。推荐Python 3.10+,支持最新async特性与库兼容。

2201_76125261的博客 1491

LeetCode解题分享:215. Kth Largest Element in an Array

Problem 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:...

DaVinciL的博客 582

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

LeetCode:215. Kth Largest Element in an Array

Medium Find thekth 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.

叶现一的专栏 159

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5.

u011514185的专栏 310

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode:215. Kth Largest Element in an Array

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 sorted order, not the kth distinct element. Example 1:...

Wisimer 192

LeetCode215: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 sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 E...

卷卷萌的博客 287

LeetCode: 215. Kth Largest Element in an Array 数组中第k大元素

试题: 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 Ex...

心之所向 313

LeetCode:215. Kth Largest Element in an Array - Python

问题描述:数组中的第K个最大元素在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。示例 1:输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2:输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。问题分析:这个题目...

Growth Diary 3075

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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2,

wengyunpeng的博客 354

leetcode:(215) Kth Largest Element in an array(java)

题目:        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 ...

Sunshine_liang1的博客 304

LeetCode: 215. Kth Largest Element in an Array

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 sorted order, not the kth distinct element. Exampl...

杨领well的专栏 173

leetcode215:Kth Largest Element in an Array

思路:参考TopK的Quick Select做法https://blog.csdn.net/Somnus_k/article/details/82724704 代码: public class KthLargestElementinanArray { public static void main(String[] args) { int[] arr = { 1, 8, 35, ...

下个路口见_20的专栏 236

Leetcode问题解答:215. Kth Largest Element in an Array

难度:medium 原链接  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. For example, Given [3,2,1,5,6,4] 

低熵随想 196

Leetcode:215. Kth Largest Element in an Array

题意:即相当于第n - k +1大的数!下面放出代码:class Solution { public: int findKthLargest(vector<int>& nums, int k) { int L = 0, R = nums.size() - 1; while (L < R) { int left = L, right =

lyy_g的博客 207
上一篇: Leetcode169: Majority Element
下一篇: Leetcode53: Maximum Subarray
sea_px
博客等级 码龄10年 1粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值