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. STL容器是否是线程安全的

    转载http://blog.csdn.net/zdl1016/article/details/5941330 STL的线程安全. 说一些关于stl容器的线程安全相关的话题. 一般说来,stl对于多线程 ...

  2. jQuery ajax - getScript() 方法和getJSON方法

    实例 使用 AJAX 请求来获得 JSON 数据,并输出结果: $("button").click(function(){ $.getJSON("demo_ajax_js ...

  3. Live555 分析(二):服务端

    live555支持单播和组播,我们先分析单播的流媒体服务端,后面分析组播的流媒体服务端. 一.单播的流媒体服务端: // Create the RTSP server: RTSPServer* rts ...

  4. 平时的笔记04:处理stagger模块

    #! /usr/bin/env python3 # # __init__.py # From the stagger project: http://code.google.com/p/stagger ...

  5. NSSCanner 提取 指定 字符串

    /** *  从msg中提取指定的内容 * *  @param msg 字符串集合 * *  @return 从msg中提取指定的内容 */ -(NSString*)extractBodyFromMe ...

  6. [Regex Expression] Tagline --- {0, } {1,10}

    Using a character set repeated 1 or more times, make a pattern to search for strings that do not con ...

  7. Win7 公布网站 HTTP 错误 404.4 - Not Found

     NET IIS7.5 创建网站时,假设发现下面错误,而且 默认网站訪问没有问题的话, 能够尝试,进入 处理程序映射 右键恢复为父级,有可能会有意想不到的 惊喜. 我的问题就是这样解决的. 出现这 ...

  8. 1. Git 克隆代码

    1. Git 克隆代码 git clone git://github.com/facebook/hiphop-php.git 2. Git更新分支 查看服务器上的所有分支 [huzg@slave3 h ...

  9. Docker快速搭建neural style环境

    ## 概览 相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/neural-style 敬请多多关注哈~~~ ## Docker镜像构建 ...

  10. 8.0 BOM对象

    主要的掌握的知识结构图 1 Window 2 控制窗口.框架.弹出窗口 3 利用location对象中的页面信息 4 使用 navigator 对象了解浏览器 1.1 BOM的核心对象是window, ...