[Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解
题目来源:https://leetcode.com/problems/kth-largest-element-in-an-array/description/
Description
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.
Solution
class Solution {
void heapFixDown(vector<int>& arr, int start, int end) {
int dad = start;
int son = dad * 2 + 1;
while (son <= end) {
if (son + 1 <= end && arr[son] < arr[son + 1]) // 选择较大子节点
son++;
if (arr[dad] >= arr[son]) { // 父节点大于子节点,堆化完毕
return;
} else { // 否则向子孙探测
swap(arr[dad], arr[son]);
dad = son;
son = dad * 2 + 1;
}
}
}
void makeHeap(vector<int>& arr, int len) {
// 从最后一个父节点开始堆化数组
for (int i = len / 2 - 1; i >= 0; i--) {
heapFixDown(arr, i, len - 1);
}
}
void heapSort(vector<int>& arr, int len) {
makeHeap(arr, len);
for (int i = len - 1; i >= 1; i--) {
swap(arr[i], arr[0]);
heapFixDown(arr, 0, i - 1);
}
}
public:
int findKthLargest(vector<int>& nums, int k) {
int len = nums.size();
heapSort(nums, len);
return nums[len - k];
}
};
解题描述
这道题我的基本思路是先对数组进行排序然后再直接获得K大的元素。排序部分使用了堆排序。
堆排序的关键步骤包含:
- 堆化数组(将数组在树中每一对父子之间都满足父亲大于儿子的要求,根节点一定是最大的数字)
- 建立最小堆(每次将根节点与最后的节点进行交换,然后将堆中最后的节点前面的节点进行堆化调整,使得每次调整完之后剩余堆中最大的数字“上浮”到根节点)
[Leetcode Week11]Kth Largest Element in an Array的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- Leetcode 之 Kth Largest Element in an Array
636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- [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 so ...
- 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 ...
- Java for 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】Kth Largest Element in an Array (middle)☆
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 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- 适用于Linux的windows子系统
Windows基于图形界面的易用性是有目共睹的,这也是很多普通用户往往难以舍弃的原因.但是Linux系统更强大的网络应用开发能力,却又是Windows系统所无法比拟的.一直以来,很多人都在试图采用各种 ...
- Cassandra 数据库设计
Cassandra 2.* CQL3.1 最近更新:2015-10-30 索引的设计 在Cassandra中经常会发现,索引不够用,不好用,各种不强大. 比如,我关注的人的需求uid + follow ...
- nohup追加日志
背景:用脚本部署Spring Boot应用,用nohup命令进行后台运行 之前的日志处理: nohup command > myout.file 2>&1 & 导致每次用s ...
- C语言分支/顺序作业总结
总结 1.1 基本要求(1分) 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 博客作业格式不规范,没有用Markdown语法 -扣分 泛泛而谈(最多七分) 1. ...
- springmvc文件上传,出现400 的错误问题
遇见的原因是公司系统上的图片上传忽然不好使了,报错400.单独针对这个模块调了好长时间都没解决,后来才发现前几天做过一个excel上传导入的功能... 使用SptingMVC3.1.3 对于文件上传提 ...
- vue里的this
vue中methods对象里的函数, this指向的都是当前实例或者组件.
- application/x-www-form-urlencoded从前端到后台
html <form id="userForm1" enctype="application/x-www-form-urlencoded" method= ...
- 【Python】- 如何使用Visual Studio 2013编写python?
安装Visual Studio 2013 1.VS2013下载安装略 安装python2.7 1.从官网下载python2.7,下载地址:https://www.python.org/getit/ ...
- 子组件通过$emit触发父组件的事件时,参数的传递
子组件.vue <template> <div> <el-table :data="comSchemaData" highlight-current- ...
- 玩lua
https://my.oschina.net/wangxuanyihaha/blog/186401