[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 ...
随机推荐
- laravel跨域问题
// 只有同源策略才允许发送cookies // header('Access-Control-Allow-Credentials:true'); 需要要index.php下开启 最近写登录图形验证码 ...
- (转) Unreal Engine 4 Custom Shaders Tutorial
说明: 1.这里的Custom Shaders 为且仅为 Custom Node的使用和USF的包含.并非全局Shader和Material Shader. 2.原文来源:https://www.ra ...
- 领扣[LeetCode]从零开始[使用C++][1,10]
0.序 以后不做后端开发是不是就用不到C++了?真香.话不多说,我已经躺倒在第一题上了.不贴题目了,持续更新. 1.两数之和 原文:https://www.cnblogs.com/grandyang/ ...
- BZOJ 1565 NOI2009 植物大战僵尸 topo+最小割(最大权闭合子图)
题目链接:https://www.luogu.org/problemnew/show/P2805(bzoj那个实在是有点小小的辣眼睛...我就把洛谷的丢出来吧...) 题意概述:给出一张有向图,这张有 ...
- Java 访问权限控制 小结
总所周知,Java提供了访问权限修饰词,以供类库开发人员向客户端程序员指明哪些是可用的,哪些是不可用的. 访问权限控制的等级,从最大权限到最小权限依次为:public.protected.包访问权限( ...
- zuoyebiji
- 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组
题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...
- 【bzoj1465/bzoj1045】糖果传递 数论
题目描述 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1个小朋友. ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- Cube 找规律
这道题我们经过简单的推测便可得知3个之前特判,四个之后就成为了一般状况,就是我们每侧都是走整个整个的|_|之后零的走|||. 考试的时候包括平时做题,许多正确的感性比理性证明要强得多. #includ ...