Kth Largest Element in an Array

Win10环境借助DockerDesktop部署Open web UI集成DeepSeek Win10环境借助DockerDesktop部署Open web UI集成DeepSeek,并借助DuckDuckGo实现联网查询 阅读详情


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大的数需要使用分治策略,可以先在数组中随机找到一个数作为目标数,用与快速排序中类似的做法,把数组中的数分成3个部分并分别存在3个向量中,分别是小于目标数的,等于目标数的和大于目标数的,然后根据k的值,确定第k大的数在哪个部分中,然后再以这个部分的向量和对应修改后的k值递归调用原函数。这样每次递归都能减少问题的规模,直到我们找到第k大的数。

如果按照最坏的情况计算,这个算法的复杂度是O(n^2),而且因为递归调用函数,会浪费大量的内存,我在开始写这个程序时,设定目标数时只是简单地把数组中的第一个元素设为目标数,导致在提交时超了内存。所以随机选取目标数中的随机非常重要,我的做法是利用一个随机数来确定目标数在数组中的下标,这样就可以保证选取的目标数是随机的,然后按照概率计算,每次选取的随机数都是最坏的情况的概率是非常低的,所以按照概率计算这个算法的复杂度是O(n logn)。

以下时源代码:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        int t;
    	vector<int> l,e,h;
    	int i=rand()%nums.size();
    	t=nums[i];
    	for(int i=0;i<nums.size();i++)
    	{
    		if(nums[i]==t) e.push_back(nums[i]);
    		else if(nums[i]<t) l.push_back(nums[i]);
    		else h.push_back(nums[i]);
    	}
    	if(h.size()>=k) 
    	{
    		l.clear();
    		e.clear();
    		return findKthLargest(h,k);
    	}
    	else if(h.size()+e.size()>=k) return t;
    	else 
    	{
    		int len1=h.size(),len2=e.size();
    		h.clear();
    		e.clear();
    		return findKthLargest(l,k-len1-len2);
    	}        
    }
};


[QMT量化交易小白入门]-七十一、十年回测ETF动量策略,超额年化收益率为10.6% 本专栏主要是介绍QMT的基础用法,常见函数,写策略的方法,也会分享一些量化交易的思路,大概会写100篇左右。QMT的相关资料较少,在使用过程中不断的摸索,遇到了一些问题,记录下来和大家一起沟通,共同进步。 阅读详情

相关推荐

SEG-Y 数据介绍及读写

无

1万+

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. For example, Given [3,2,1,5,6,4] and k = 2, return 5.

KaJIU的博客 451

三相电机控制入门:端电压、相电压、线电压到底怎么测?(附实测波形对比)

本文为三相电机控制初学者提供了端电压、相电压和线电压的完整测量指南。通过厘清三者的物理定义与测量方法,详细讲解了如何搭建虚拟星形点电路,并对比分析了方波、SPWM和SVPWM三种主流控制模式下关键电压波形的核心特征与差异,帮助工程师掌握正确的示波器测量与波形解读技巧。

oauth7security的博客 671

215. Kth Largest Element in an Array

215. Kth Largest Element in an Array

weixin_45428910的博客 733

Silvaco Atlas入门指南——从零开始掌握半导体器件仿真

本文为Silvaco Atlas半导体器件仿真软件的入门指南,详细介绍了从零开始掌握该工具的核心流程。内容涵盖Atlas的基本概念、仿真流程、核心文件(输入、结构、输出文件)、交互式与批处理两种操作模式,并逐步演示了如何从定义网格、划分区域、设置电极到进行掺杂、选择物理模型并求解,最终获取和分析器件特性的完整步骤。旨在帮助微电子相关专业人员和工程师快速上手这一强大的“数字实验室”,提升半导体器件设计与研发效率。

weixin_29306317的博客 409

Android开发Intent详解

本文详解Android开发Intent。

qq_30725967的博客 2419

【leetcode每日刷题】215. Kth Largest Element in an Array

找第k大的数 1、使用最小堆,当大于k的时候并且插入的值大于堆顶元素的时候,删除堆顶元素,在把新元素加入堆中。 public int heap(int[] nums, int k) { PriorityQueue<Integer> heap = new PriorityQueue<>(); for(int num:nums){ ...

菜菜鸟的博客 280

YOLOv11 改进 - 特征融合 | 小目标检测救星:DySample动态上采样减少特征丢失

本文介绍了超轻量且高效的动态上采样器DySample与YOLOv11的结合,以解决传统上采样器工作负载大、应用场景受限的问题。DySample从点采样角度构建上采样,绕过动态卷积,使用PyTorch标准内置函数实现,无需定制CUDA包,能大幅减少参数、FLOPs等资源消耗。我们将DySample代码集成进YOLOv11。实验脚本显示,结合DySample的YOLOv11在多个密集预测任务中或表现出色。

魔改工程师的博客 1429

LeetCode 题解(118): 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, retur

257

三维重建/SLAM笔记_最佳学习路线/攻略

本文目的 学习三维重建,需要不少知识,而知识之间又存在不同的依赖关系,为了后来人能少走弯路,也为了对自己的知识框架系统总结,特记录自己的学习路线。 学习心得 关于读书,了解基础知识是一方面,产生疑问也是一方面,为以后知道学习什么留下印象; 一回生,二回熟,看不懂时,大多是因为先验知识不够,没关系,多查多想,总会搞懂的。 学习路线 1.计算机图形学基础(OpenGL基础版)(徐文鹏 清...

惊鸿一博 3297

HMC7044调试指南与FPGA开发

最后,我们介绍了在FPGA开发中使用HMC7044的几个常见应用场景,包括时钟分配、时钟同步、高精度时钟生成和时钟域交叉。根据您的应用需求,您可以选择不同的时钟分配方案,例如使用PLL生成特定频率的时钟信号,或者通过分频器生成较低频率的时钟信号。HMC7044可以提供精确的时钟同步功能,确保各个FPGA板之间的时钟信号保持一致,从而实现可靠的数据传输和协同操作。HMC7044可以实现时钟域之间的交叉,通过生成和分配不同频率的时钟信号,使得不同时钟域的模块能够协同工作。FPGA开发中的HMC7044应用。

CqpFsharp的博客 3629

Leetcode-215. Kth Largest Element in an Array

地址:https://leetcode.com/problems/kth-largest-element-in-an-array/ 描述: Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth...

Lost2013的博客 212

基于Qt C++开发一套大疆农业无人机的AI飞控系统

如果需要补充**多机协同作业代码**、**农田测绘数据导出**、**作业数据统计大屏**或**移动端地面站适配**功能,可以告知具体需求,我会针对性完善。"开启" : "关闭") << "喷洒,流量:" << flowRate << "L/min";emit errorOccurred(QString("SDK初始化失败,错误码:%1").arg((int)ret));report += QString("问题区域数量:%1\n").arg(info.problemAreas.size());

寒寒豆豆软件公司的博客 397

#Leetcode# 215. Kth Largest Element in an Array

https://leetcode.com/problems/kth-largest-element-in-an-array/ Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct ...

weixin_30262255的博客 76

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

[LeetCode] Kth Largest Element in an Array

Find the kth largest element in an unsorted array. 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. https://leetcode.co...

weixin_34233421的博客 87

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Kth Largest Element in an Array Leetcode #215 题解[C++]

题目来源 https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目理解 ind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted or...

Wayne_Mai的博客 433

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

【leetcode】215. Kth Largest Element in an Array

215.Kth Largest Element in an Array 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:...

lemonade13的博客 164

【Leetcode】215. 数组中第 k 大的数字(Kth Largest Element in an Array)

Leetcode - 215 Kth Largest Element in an Array (Medium) Input: [3,2,1,5,6,4] and k = 2 Output: 5 解法一:排序 public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.leng...

厉兵秣码 257

212. Word Search II && 215. Kth Largest Element in an Array

212. Word Search II Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” ...

机器学习,大数据 256

leetcode 215. Kth Largest Element in an Array 数组中的第K大的元素

https://leetcode.com/problems/kth-largest-element-in-an-array/ 将数组看为没有调整到位的最大堆,由于堆是完全二叉树,因此根节点与子节点的下标存在对应关系。 每次从堆中取出最大值,将其放至根节点,再与最后一个节点互换位置,然后将其从堆删除。 class Solution { public: int findKthLar...

rock4you 269

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

【快排】数组第k大元素 LeetCode - 215. Kth Largest Element in an Array

  快排的详细总结可以看   快排的思想就是,每次选一个 pivot(比如最后一个数),然后将比它小的放它左边(或者右边),把比它大的放他右边(或者左边),相当于每次排好一个数,然后递归的排他左右两部分,这个数自己就不会再动了。   面试时经常回问的一道题就是,找出一个数组第 k 大的元素: LeetCode - 215. Kth Largest Element in an Array Find...

Bob__yuan的博客 353
上一篇: Search a 2D Matrix II
下一篇: Median of Two Sorted Arrays
algdecs
博客等级 码龄10年 0粉丝 22原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值