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大的元素。排序部分使用了堆排序。

堆排序的关键步骤包含:

  1. 堆化数组(将数组在树中每一对父子之间都满足父亲大于儿子的要求,根节点一定是最大的数字)
  2. 建立最小堆(每次将根节点与最后的节点进行交换,然后将堆中最后的节点前面的节点进行堆化调整,使得每次调整完之后剩余堆中最大的数字“上浮”到根节点)

[Leetcode Week11]Kth Largest Element in an Array的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

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

  3. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

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

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

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

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

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

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

随机推荐

  1. 第二十三篇 logging模块(******)

    日志非常重要,而且非常常用,可以通过logging模块实现. 热身运动 import logging logging.debug("debug message") logging. ...

  2. python3 安装win32clipboard 和 win32con 报No matching distribution found for win32con错误

    win32con.win32clipboad不能用pip install 安装,也不能够查找到这个包,原来,这个是pypiwin32的一部分,直接安装pypiwin32就可以了 pip install ...

  3. Spring实战第六章学习笔记————渲染Web视图

    Spring实战第六章学习笔记----渲染Web视图 理解视图解析 在之前所编写的控制器方法都没有直接产生浏览器所需的HTML.这些方法只是将一些数据传入到模型中然后再将模型传递给一个用来渲染的视图. ...

  4. LeetCode 876——链表的中间结点

    1. 题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形 ...

  5. 测试理论--branch testing and boundary testing

    1 branch testing 分支测试 测试代码的所有分支 2 boundary testing 测试 程序的限制条件

  6. 软工实践 - 第二十七次作业 Beta 冲刺(5/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10134471.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...

  7. asp.net文件上传进度条研究

    文章:asp.net 文件上传进度条实现代码

  8. 第一个Spring小程序实战

    ps:本文偏向原理和操作性,原理适合于任何编译器. 支持Spring入门,目的是在xml文件里面装配相关bean(java对象),并实现获取.(IOC) 一.先建立一个Spring新项目,添加mave ...

  9. poj 1753 Flip Game (dfs)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28805   Accepted: 12461 Descr ...

  10. BZOJ1927 [Sdoi2010]星际竞速 【费用流】

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 2582  Solved: 1601 [Submit][St ...