LeetCode215.Kth Largest Element in an Array

【OpenBMC 系列】4.启动流程 使用qume模拟ast2600-evb 1.安装openbmc适配的qume 2.启动bmc 3.使用bmc web 阅读详情

题目:

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

思路:

用最大堆,每次取完一个堆顶就重新构建一次。用的是以前写堆排序的代码。

题解:

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        # 调整堆
        def adjust_heap(lists, i, size):#以i为根节点开始调整成以i为根节点的最大堆
            lchild = 2 * i + 1#堆是完全二叉树,可求左右子树
            rchild = 2 * i + 2
            max = i
            if i < size / 2:#完全二叉树的分支节点(就是有叶子节点的)小于等了size/2
                if lchild < size and lists[lchild] > lists[max]:
                    max = lchild
                if rchild < size and lists[rchild] > lists[max]:
                    max = rchild
                if max != i:#如果根节点不是最大就调换位置
                    lists[max], lists[i] = lists[i], lists[max]
                    adjust_heap(lists, max, size)#然后以max为根节点进行调整

        # 创建堆
        def build_heap(lists, size):#从下往上构建堆
            for i in range(0, int(size/2))[::-1]:
                adjust_heap(lists, i, size)
        
        size = len(nums)
        build_heap(nums,size)#先创建堆
        count = 0
        for i in range(0, size)[::-1]:#i从size-1到1
            nums[0], nums[i] = nums[i], nums[0]#交换堆顶和堆底
            count += 1 #获得一个最大值加1
            if count == k:
                #print(nums[i])
                return nums[i]
            adjust_heap(nums, 0, i)#把剩下的重新排

 

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况 主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。 立即下载

相关推荐

SpotBugs使用

spotbugs命令行参数解析

javajingling的专栏 2956

215. 数组中的第K个最大元素(排序算法)

215. 数组中的第K个最大元素 难度中等984收藏分享切换为英文接收动态反馈 在未排序的数组中找到第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 ≤ 数组的长度。 1.#冒泡排序(超时)O(n^2)最好O(N)...

a707931675的博客 269

别再傻傻分不清!用R语言5分钟搞定RR、AR、OR的计算与解读(附代码)

本文通过R语言实战演示,详细讲解了RR(相对风险)、AR(绝对风险)和OR(比值比)的计算方法与科学解读。提供完整的代码示例和可视化技巧,帮助医学研究者快速掌握这些核心流行病学指标的应用与常见陷阱规避。

weixin_42526015的博客 270

代码:数组第K(大/小)

思路一:库函数 //数组第K大 class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: return sorted(nums, reverse = True)[k - 1] //数组第K大 class Solution: def findKthLargest(self...

weixin_44595042的博客 221

Amesim基础篇-元件详解-流体库管路

Amesim基础篇-元件详解-流体库管路

2201_75375391的博客 4531

LAMMPS实战:如何用ReaxFF力场模拟碳氢化合物热解(附完整脚本)

本文提供了一份使用LAMMPS软件中ReaxFF反应力场模拟碳氢化合物热解过程的完整实战指南。内容涵盖从环境准备、力场参数选择、能量最小化,到升温恒温动力学模拟的完整脚本编写,并介绍了基于轨迹文件的产物分析方法,帮助计算化学与材料科学研究者快速上手这一关键的分子动力学仿真技术。

soda5的博客 1136

Find Elements in Matrix

Lintcode 737已知: 给定一个二维数组,求出在二维数组每一行都出现过的一个数字(假定这个数字是唯一的) 示例:[ [2,5,3], [3,2,1], [1,3,5] ] //返回值为3思路: 这个问题本身应该难度不高,难点应该也是在于如何把多层循环转换成递归。我们可以假定当前为第一行的第N个数,然后从第二行找有没有相等的数,如果有,则递归至第三层,依次递归到最后一层仍然为

Mio_Bass的博客 310

离线安装 nvidia-docker2(nvidia-container-toolkit)

很多时候大家都有用docker使用gpu的需求,但是因为网络等原因不是那么好用,这里留了一个给ubuntu的安装包,网络好的话也提供了在线安装方式。

father_yingying的博客 1187

Leetcode 215. Kth Largest Element in an Array(python 快速排序)

class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: end = len(nums) - 1 self.quick_sort(nums, 0, end) return nums[-k] ...

ASKCOS博客 225

windows下宝塔安装教程

windows下宝塔安装教程 目录宝塔是什么?宝塔安装流程1.下载2.配置3.登录4.安装的环境套件小结 宝塔是什么? 宝塔是管理服务器的免费的软件,宝塔上面可以进行对你的服务器批量进行管理,对于运维人员来说还是必不可少的一个软件,值得使用。 宝塔安装流程 1.下载 链接: https://download.bt.cn/win/panel/BtSoft.zip 下载后安装买最好自定义目录,方便找 2.配置 下载完成后会出现这个页面,对着页面进行配置 点击默认密码复制进来,这里注意一点就是一定要查看

你在看屏幕的同时,屏幕也在注视着你 4437

【skill】漫剧 AI 视频创作的知识库 + API 调用手册

方案适用场景在对话中使用,自动触发注入到任何 LLM(Cursor/Windsurf/Claude API)Python 模块集成到自动化管线/批量生成GitHub 又限速了,不过前几轮搜到的已经够用了。根据搜索结果,结合已分析过的项目,给你整理完整对比 🐮。

突围 1055

lintcode---Find Elements in Matrix

题目描述: Given a matrix, find a element that appear in all the rows in the matrix.You can assume that there is only one such element.样例: For example: Given a matrix:[ [2,5,3], [3,2,1], [1,3,5] ]r

一直在路上的羊的博客 404

Open3D KD-tree的使用【2025最新版】

Open3d使用FLANN构建KDTree以便进行快速最近邻检索。博客长期更新,本文最近一次更新时间为:2025年1月1日,代码已同步更新至Open3D 0.18.0版本。

点云侠的博客 40万+

【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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

【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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

leetcode 215: Kth Largest Element in an Array

leetcode 215: Kth Largest Element in an Array

西施豆腐渣 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

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

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

【Leetcode】215. Kth Largest Element in an Array

大元素(其定义是从大到小排好序后位于第。个的数,即如果下标从。

数学、算法爱好者的博客 434

[leetcode] 215. Kth Largest Element in an Array 解题报告

题目链接:https://leetcode.com/problems/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 dis

小榕流光的专栏 2548

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

中华小当家的博客 188

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

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

xxxnnn1991的博客 461

leetcode 215 Kth Largest Element in an array

目录leetcode215 Kth Largest Element in an array问题描述算法分析解法一:O(n)快速选择解法二:O(nlog(n)) 最小堆实现步骤代码实现快速查找最小堆 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 ord

心安成长的码字书房 248

leetcode 215. Kth Largest Element in an Array

目录 一、问题分析 二、代码实现 1、排序 采用内置排序 采用冒泡排序 基数排序 2、堆 采用大顶堆 采用小顶堆 3、快速选择 递归版本 迭代版本 将输入数组随机化 采用中间下标的元素作为参照元素 https://leetcode.com/problems/kth-largest-element-in-an-array/ 返回数组中第k大的元素 一、问...

小景的博客 699
上一篇: LeetCode20. Valid Parentheses
下一篇: LeetCode23. 合并K个排序链表
Clark_Xu
博客等级 码龄12年 98粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值