#leetcode#Kth Largest Element in an Array
2015-06-29 15:20
344 查看
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
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
方法一: Arrays.sort( nums ), 则 nums[nums.length - k]就是 kth largest O(nlogn)
方法二: 维护一个大小为 k 的 min heap, 所有元素从heap里走一遍, 则最后 heap.peek()就是 kth largest
方法三: quick select,注意 partition 的时候 左指针 <= pivot 都向右走, 扫描完一遍之后把 pivot 放到他的正确位置上,右指针位置为第一个小于比pivot大的值的位置, 即右指针位置是pivot的正确位置
时间复杂度 on average O(n), worst case O(n^2 )
// quick select
public class Solution {
public int findKthLargest(int[] nums, int k) {
if(nums == null || nums.length == 0){
return 0;
}
return findKthLargest(nums, k, 0, nums.length - 1);
}
private int findKthLargest(int[] nums, int k, int start, int end){
int pivot = start;
int left = start;
int right = end;
while(left < right){
// after this while loop, nums[left] will be the first element from the left that is > pivot
while(left < nums.length && nums[left] <= nums[pivot]){
left++;
}
// after this while loop, nums[right] will be the first element from the right that is < pivot
while(right >= 0 && nums[right] > nums[pivot]){
right--;
}
if(left < right)
swap(nums, left, right);
}
swap(nums, pivot, right); // move nums[pivot] to its correct position
if(right == nums.length - k){
return nums[right];
}else if(right > nums.length - k){
return findKthLargest(nums, k, start, right - 1);
}else{
return findKthLargest(nums, k, right + 1, end);
}
}
private void swap(int[] A, int l, int r){
int tmp = A[l];
A[l] = A[r];
A[r] = tmp;
}
}
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.
方法一: Arrays.sort( nums ), 则 nums[nums.length - k]就是 kth largest O(nlogn)
方法二: 维护一个大小为 k 的 min heap, 所有元素从heap里走一遍, 则最后 heap.peek()就是 kth largest
方法三: quick select,注意 partition 的时候 左指针 <= pivot 都向右走, 扫描完一遍之后把 pivot 放到他的正确位置上,右指针位置为第一个小于比pivot大的值的位置, 即右指针位置是pivot的正确位置
时间复杂度 on average O(n), worst case O(n^2 )
// quick select
public class Solution {
public int findKthLargest(int[] nums, int k) {
if(nums == null || nums.length == 0){
return 0;
}
return findKthLargest(nums, k, 0, nums.length - 1);
}
private int findKthLargest(int[] nums, int k, int start, int end){
int pivot = start;
int left = start;
int right = end;
while(left < right){
// after this while loop, nums[left] will be the first element from the left that is > pivot
while(left < nums.length && nums[left] <= nums[pivot]){
left++;
}
// after this while loop, nums[right] will be the first element from the right that is < pivot
while(right >= 0 && nums[right] > nums[pivot]){
right--;
}
if(left < right)
swap(nums, left, right);
}
swap(nums, pivot, right); // move nums[pivot] to its correct position
if(right == nums.length - k){
return nums[right];
}else if(right > nums.length - k){
return findKthLargest(nums, k, start, right - 1);
}else{
return findKthLargest(nums, k, right + 1, end);
}
}
private void swap(int[] A, int l, int r){
int tmp = A[l];
A[l] = A[r];
A[r] = tmp;
}
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)