《leetCode》:Kth Largest Element in an Array

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南 阅读详情

题目

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大的元素即可。但是,时间复杂度为O(n*logn)

int cmp(const void* a,const void* b){
    return  (*((int *)a))-(*((int *)b));
}

int findKthLargest(int* nums, int numsSize, int k) {
    if(nums==NULL||numsSize<1||k<0||k>numsSize){
        return 0;
    }
    qsort(nums,numsSize,sizeof(nums[0]),cmp);
    return nums[numsSize-k];
}

思路二:不完全排序

思路一是全部将数组进行了排序然后选择出结果。其实,我们可以选择不完全排序来解决问题,在快排中,将数组分成两半,前面一半数组中的数全部小于 主元的值,后面一半数组中的数全部大于主元的值,如果主元的索引index

void swap(int *a,int *b){
    int temp=*a;
    *a=*b;
    *b=temp;
}
int partition(int* nums, int start,int end){
    if(end<start){
        return -1;
    }
    int val=nums[start];
    int index=start;
    for(int i=start+1;i<=end;i++){
        if(nums[i]<val){
            index++;
            swap(&nums[index],&nums[i]);
        }
    }
    swap(&nums[start],&nums[index]);
    return index;
}

void sortHelper(int* nums, int start,int end,int numsSize,int k){
    if(end<=start){
        return;
    }
    int index=partition(nums,start,end);
    if(index==numsSize-k){
        return;
    }
    else if(index<numsSize-k){
        sortHelper(nums,index+1,end,numsSize,k);
    }
    else if(index>numsSize-k){
        sortHelper(nums,start,index-1,numsSize,k);
    }
}

int findKthLargest(int* nums, int numsSize, int k) { 
    if(nums==NULL||numsSize<1||k<0||k>numsSize){
        return 0;
    } 
    int start=0;
    int end=numsSize-1;
    sortHelper(nums,start,end,numsSize,k);
    return nums[numsSize-k];
}

通过上面的理论分析,理论上第二种方法的运行时间会比第一种的运行时间少,出于好奇的看了下这两种方法的运行时间,如下:

第一种方法的运行时间:

第二种方法的运行时间:

从两张图中可以看出,居然是第一种的还要快。比较郁闷,居然和理论结果不一样。
郁闷的同时也开始找原因,开始以为是因为函数调用开销太大,导致第二种方法的运行时间过慢,因此,就把swap(int *a,int *b) 的功能全部用三行代码在函数partition中实现。居然,根本没有改善。呜呜。

全球首发48V架构的Cybertruck,到底牛在哪?| 深扒特斯拉高压系统架构 特斯拉Cybertruck作为全球首款标配48V架构的量产车,在电气系统架构上进行了多项创新。首先,它采用以太网连接高压控制器与后域控制器,提升了数据传输效率和智能化水平。其次,Cybertruck引入了Pyro烟火断开装置,用于碰撞时快速断开电池连接,提高安全性。在电流监测方面,特斯拉既保留了传统的Shunt分流器技术,又引入了更先进的磁通门电流传感器,后者具有无温漂、完全隔离等优势。48V供电系统在降低电流损耗方面表现优异,但也面临电弧风险、盐水腐蚀等挑战。Cybertruck的48V系统通过PCS供电 阅读详情

相关推荐

从TCGA官网获取新版count数据并精准转换为TPM的实战指南

本文详细介绍了从TCGA官网获取新版count数据并精准转换为TPM的实战指南。通过TCGAbiolinks工具下载原始count数据,结合GENCODE v36注释文件计算基因有效长度,最终实现count到TPM的标准化转换,确保数据完整性和分析可靠性。文章包含完整代码示例和常见问题解决方案,特别适合生物信息学研究人员参考。

weixin_27310417的博客 286

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

Cortex-M TrustZone实战:从硬件隔离到安全交互的嵌入式开发指南

在嵌入式系统开发中,硬件安全隔离是构建可信执行环境(TEE)的核心技术。其原理是通过处理器硬件机制,将系统资源划分为安全与非安全域,实现内存、外设和代码的强制访问控制。这项技术的核心价值在于,它能在单一芯片上为资源受限的物联网(IoT)设备、智能传感器等提供芯片级的安全基础,无需依赖外置安全元件,从而降低BOM成本并简化设计。典型的应用场景包括保护设备密钥、实现安全启动、构建安全服务(如加密、密钥管理)以供应用程序调用。本文聚焦于Arm Cortex-M系列微控制器上的TrustZone技术实现,针对开发中

weixin_42639246的博客 302

215. Kth Largest Element in an Array

215. Kth Largest Element in an Array

weixin_45428910的博客 733

51单片机-点亮LED和蜂鸣器

ULN2003是高耐压、大电流达林顿陈列,由七个硅NPN达林顿管组成。ULN2003是大电流驱动陈列,多用于单片机、只能仪表、PLC、数字量输出卡等控制电路中。可直接驱动蜂鸣器、继电器等负载。void delay(u16 i) // i等于1大约10uswhile(i--);//取反,高低电平不断变化,产生脉冲delay(10);// 周期200us。

三日沐水 1135

二维卡尔曼滤波位置,速度融合

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Ma...

Johnlee2019的博客 8456

【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

Coze视频混剪智能体工作流,1小时制作100条带货混剪视频,全平台通用

摘要:本文介绍如何利用Coze智能体工作流一键生成全平台带货混剪视频。该工作流支持双视频素材查重处理、智能文案重塑、画面重组等核心功能,通过多节点协同实现批量生产。系统可自动下载视频、提取字幕、生成新脚本,并完成剪辑合成,支持飞书表格批量操作。该工作流已在iThinkAi扣子团队空间开放复制使用,帮助用户高效制作爆款带货视频。

ai979290的博客 1092

Kth Largest Element in an Array

Find K-th largest element in an array. Example Example 1: Input: n = 1, nums = [1,3,4,2] Output: 4 Example 2: Input: n = 3, nums = [9,3,2,4,8] Output: 4 Challenge O(n) time, O(1) extra memo...

leetcode 解题思路 650

【swjtu】算法实验2.4分析算法时间复杂度

分析给定算法的时间复杂度。 上机实验,绘制不同输入情况下的运行时间曲线,并与(1)中的分析结果进行比较。 撰写相应的实验报告,实验报告内容包括:实验目的、实验任务、 实验环境、实验步骤、实验结果及其分析以及实验总结等部分内容。

qq_52046937的博客 735

Android系统开发工程师面试指南与参考答案

本文整理了Android系统开发工程师面试常见问题与参考答案。核心内容涵盖:1)Framework层定制方法,如修改HAL和系统服务;2)ANR问题分析工具(systrace)与解决方案;3)性能优化实践,包括开机加速和功耗管理;4)四大组件工作机制与应用案例;5)Native崩溃调试技巧。文章强调通过AOSP源码研究、工具使用和项目案例准备来提升面试表现,重点考察应聘者的系统开发深度和实操能力。参考答案均基于实际开发经验,提供可量化的优化成果说明。

郑伟强dev的专栏 262

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

6、基因组数据可视化与甲基化分析:从基础操作到深入洞察

本文详细介绍了基因组数据可视化和全基因组DNA甲基化分析的方法和技术,包括基因组查看器(IGV)的基本操作、单核苷酸变异的可视化、DNA甲基化与CpG岛的关联分析、统计建模与显著甲基化区域识别,以及不同细胞系之间的甲基化模式差异分析。通过具体案例展示了如何利用MATLAB进行数据分析和可视化,为基因组研究、疾病机制探索及个性化医疗提供了技术支撑和实践参考。

qsc901234的博客 297

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.

哈哈的个人专栏 3872

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

leetcode 215: Kth Largest Element in an Array

leetcode 215: Kth Largest Element in an Array

西施豆腐渣 1万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

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

xxxnnn1991的博客 461

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode:Kth Largest Element in an Array

Kth Largest Element in an Array Total Accepted:59659Total Submissions:175932Difficulty:Medium Find thekth largest element in an unsorted array. Note that it is the kth la

walker lee 1623

Leetcode: 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

YMHH的专栏 500

Leetcode215: Kth Largest Element in an Array

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 e

~\(≧▽≦)/~ 359

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

Lost Monkey 455

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

LeetCode:Kth Largest Element in an Array

LeetCode,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,

小贤同学的博客 318

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

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

LeetCode 题解(120): 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

675

Leetcode 0215: 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 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Out

weixin_43946031的博客 188
上一篇: 《leetCode》:Isomorphic Strings
下一篇: 《JAVA源码分析》:ArrayList
HelloWorld_EE
博客等级 码龄14年 1860粉丝 609原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值