Question

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 1 -- PriorityQueue

1. 将所有元素放入heap中

2. 依次用poll()取出heap中最大值

 public class Solution {
public int findKthLargest(int[] nums, int k) {
if (nums == null || k > nums.length)
return 0;
int length = nums.length, index = length - k, result = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length,
new Comparator<Integer>(){
public int compare(Integer a, Integer b) {
return (a - b);
}
});
for (int i = 0; i < length; i++)
pq.add(nums[i]);
while (index >= 0) {
result = pq.poll();
index--;
}
return result;
}
}

Improvement

 public class Solution {
public int findKthLargest(int[] nums, int k) {
if (nums == null || k > nums.length)
return 0;
int length = nums.length, index = k, result = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length, Collections.reverseOrder());
for (int i = 0; i < length; i++)
pq.add(nums[i]);
while (index > 0) {
result = pq.poll();
index--;
}
return result;
}
}

Solution 2 -- Quick Select

Quick Sort in Java

Average Time O(n)

 '''
a: [3, 2, 5, 1], 2
output: 2
''' def swap(a, index1, index2):
if index1 >= index2:
return
tmp = a[index1]
a[index1] = a[index2]
a[index2] = tmp def partition(a, start, end):
''' a[start:end]
pivot: a[end]
return: index of pivot
'''
pivot = a[end]
cur_big = start - 1
for i in range(start, end):
if a[i] >= pivot:
cur_big += 1
swap(a, cur_big, i)
cur_big += 1
swap(a, cur_big, end)
return cur_big def quick_select(a, k):
k -= 1
length = len(a)
start = 0
end = length - 1
while start < end:
index = partition(a, start, end)
if index == k:
return a[index]
if index > k:
# Note: end != index
end = index - 1
else:
# Note: start != index
start = index + 1
k = k - index
return -1 a = [3, 2, 5, 1]
k = 1
print(quick_select(a, k))

Kth Largest Element in an Array 解答的更多相关文章

  1. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  2. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

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

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

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

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

  6. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  7. Lettcode Kth Largest Element in an Array

    Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...

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

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

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

随机推荐

  1. C/C++内存存储问题

    #include <stdio.h> #include "string.h" #include "malloc.h" void Swap(int a ...

  2. 【POJ2196】Specialized Four-Digit Numbers(暴力打表)

    一道水题,只要会复制粘贴就好! #include <iostream> #include <cstring> #include <cstdlib> #include ...

  3. Python Cookie HTTP获取cookie并处理

    Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...

  4. Linux硬盘分区和格式化

    分区与格式化   先用fdisk分区,分区完成后再用mkfs格式化并创建文件系统,挂载,磁盘就能使用啦.   分区的原理:        MBR:主引导扇区 主分区表:64bytes,最多只能分四个主 ...

  5. Chosen 基本使用

    点击下载Chosen 引入文件 chosen.css jquery-1.7.1.min.js chosen.jquery.js 绑定数据: for (var i = 0; i < data.le ...

  6. Javascript 中的变量

    var a; console.log("The value of a is " + a); // The value of a is undefined console.log(& ...

  7. SmaterWeatherApi---签名加密和数据訪问--简单粗暴一步搞定

    -----------------------------------------------------更新-2014-07-09---------------------------------- ...

  8. 智能路由——ESB

    SOA之我见 SOA已然是企业级开发的必定之路.有人会问:我们有了OOP,还须要SOA吗?好吧我承认,这个问题也困扰了我非常久.现现在我得出的结论是:OOP是OOP,SOA是SOA. OOP是指面向对 ...

  9. Java中随机数生成的两种方法,以及math的floor

    1.Math的random方法,调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范围是[0.0,1.0)的左闭右开区间,返回值是一个伪随机选 ...

  10. Android 打造自己的个性化应用(五):仿墨迹天气实现续--> 使用Ant实现zip/tar的压缩与解压

    上一篇中提到对于Zip包的解压和压缩需要借助Ant 实现,我经过参考了其他的资料,整理后并加上了一些自己的看法: 这里就具体地讲下如何使用Ant进行解压缩及其原因: java中实际是提供了对  zip ...