LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/
Submissions: 136063 Difficulty: Medium
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.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
求给定数组的第K大的元素。
假设先排序,然后取第K个元素,那么时间复杂度是O(n*log n)。
借助堆的数据结构,能够把时间复杂度降到O(n*logk)。
假设求第K大的元素,那么要构建的是小顶堆。
求第K小的元素。那么要构建大顶堆!先构建k个元素的堆。另外i-k个元素逐个跟堆顶元素比較。假设比堆顶元素小,那么就将该元素纳入堆中,并保持堆的性质。
我的AC代码
public class KthLargestElementinanArray {
public static void main(String[] args) {
int[] a = { 3, 2, 1, 5, 6, 4 };
System.out.println(findKthLargest(a, 1));
int[] b = { -1,2,0};
System.out.println(findKthLargest(b, 3));
int[] c = { 3,1,2,4};
System.out.println(findKthLargest(c, 2));
}
public static int findKthLargest(int[] nums, int k) {
int[] heap = new int[k];
heap[0] = nums[0];
for (int i = 1; i < k; i++) {
siftUp(nums[i], heap, i);
}
for (int i = k; i < nums.length; i++) {
siftDown(nums, k, heap, i);
}
return heap[0];
}
private static void siftDown(int[] nums, int k, int[] heap, int i) {
if (nums[i] > heap[0]) {
heap[0] = nums[i];
int p = 0;
while(p < k) {
int minChild = 2 * p + 1;
if(minChild + 1 < k && heap[minChild] > heap[minChild + 1]) minChild ++;
if(minChild < k && heap[p] > heap[minChild]) {
swap(heap, p, minChild);
p = minChild;
} else break;
}
}
}
private static void siftUp(int num, int[] heap, int i) {
int p = i;
heap[i] = num;
while (p != 0) {
int parent = (p - 1) / 2;
if (heap[parent] > heap[p]) {
swap(heap, p, parent);
}
p = parent;
}
}
private static void swap(int[] heap, int p, int parent) {
int temp = heap[parent];
heap[parent] = heap[p];
heap[p] = temp;
}
}
LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解的更多相关文章
- LeetCode OJ 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】215. Kth Largest Element in an Array (2 solutions)
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- 【刷题-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- 【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 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
随机推荐
- [Spring实战系列](17)编写切点与声明切面
切点用于准确定位应该在什么地方应用切面的通知. 切点和通知是切面的最基本元素. 在Spring AOP中,须要使用AspectJ的切点表达式语言来定义切点. 关于Spring AOP的AspectJ切 ...
- Threejs 官网 - 怎样执行本地的东西(How to run things locally)
Threejs 官网 - 怎样执行本地的东西(How to run things locally) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循&qu ...
- 非Qt工程使用Qt的信号槽机制
非Qt工程,使用Qt的信号槽机制,蛋疼不?反正我现在就是要做这样一件蛋疼的事. 要使用Qt的信号槽机制,下面是从Qt Assist里面关于 signal & slots 的一句介绍: All ...
- elasticsearch源码分析之search模块(server端)
elasticsearch源码分析之search模块(server端) 继续接着上一篇的来说啊,当client端将search的请求发送到某一个node之后,剩下的事情就是server端来处理了,具体 ...
- Caffe C++API 提取任意一张图片的特征系列二----MemoryData
介绍一种更加灵活的方法,用MemoryData层输入数据,可以直接用opencv接口读入我们的图片再添加的网络中. 第一个问题:仍然是工程建立问题,提示卷积层或其他层没有注册,解决方法与上一篇博客一 ...
- oracle 11gR2 如何修改vip
因为业务需要,需要将p570a主机和p570b主机上的vip做修改 修改前ip 192.168.128.12 p570a-vip 192.168.128.13 p570b-vip 修改后ip ...
- Repeater 中 OnItemCommand 用法
<table> <asp:Repeater ID="rptList" runat="server"OnItemCommand="rp ...
- GRpc-Go使用笔记
linux下配置GRpc-golang 1.git中下载protobuf包 2.解压(/usr/local/protobuf) unzip protobuf-cpp-3.0.0-alpha-3.z ...
- 用IIS怎样在局域网内建网站
IIS服务器组建一览 IIS(Internet Information Server,互联网信息服务)是一种Web(网页)服务组件,其中包括Web服务器.FTP服务器.NNTP服务器和SMTP服务器, ...
- Django中ORM之操作表记录
添加表记录 添加普通字段 #方法一 book_obj = Book(title='book7',publishDate='2011-05-02',price=200,publish_id=1) boo ...