leetcode 215. Kth Largest Element in an Array
2017-08-15 12:07
337 查看
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.
又是一道很经典的问题。
我用的最小堆做的。
接着令 k=nums.length-k ( 第 k 大的数 ,是第 nums.length-k 索引小的数)
如 [3,2,1,5,6,4] ,第2大的数是5,把小于等于5的数放到左边后,[3,2,1,4,5,6],5的索引是4,符合6-2=4
Time complexity = O(n)
每次抛弃一半数组: n+(n/2)+(n/4)..1 = n + (n-1) = O(2n-1) = O(n), because n/2+n/4+n/8+..1=n-1.
Quick Select Solution Code:
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.
又是一道很经典的问题。
我用的最小堆做的。
public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> queue=new PriorityQueue<Integer>(); for(int i=0;i<nums.length;i++){ if(queue.size()<k){ queue.offer(nums[i]); } else{ int kLargest=queue.peek(); if(kLargest<nums[i]){ queue.poll(); queue.offer(nums[i]); } } } return queue.peek(); }大神表示:这道题可以用quickSelect算法(类似于quicksort中的划分方法),时间复杂度是O(n)。
把 numbers < pivot 放到 pivot 的左侧 把 numbers > pivot 放到 pivot 的右侧
接着令 k=nums.length-k ( 第 k 大的数 ,是第 nums.length-k 索引小的数)
如 [3,2,1,5,6,4] ,第2大的数是5,把小于等于5的数放到左边后,[3,2,1,4,5,6],5的索引是4,符合6-2=4
//如[1,2,3],第二大的数为2,将比2小的数放到2左边后,对应索引为index=3-2=1 if indexOfPivot == k, return A[k] else if indexOfPivot < k, keep checking right part to pivot else if indexOfPivot > k, keep checking left part to pivot
Time complexity = O(n)
每次抛弃一半数组: n+(n/2)+(n/4)..1 = n + (n-1) = O(2n-1) = O(n), because n/2+n/4+n/8+..1=n-1.
Quick Select Solution Code:
public int findKthLargest(int[] nums, int k) { if (nums == null || nums.length == 0) return Integer.MAX_VALUE; return findKthLargest(nums, 0, nums.length - 1, nums.length - k); } public int findKthLargest(int[] nums, int left, int right, int k) {// quick select: kth smallest if (left > right) return Integer.MAX_VALUE; int pivot = nums[right];// Take A[right] as the pivot, int low = left; for (int i = left; i < right; i++) { //把 所有小于等于pivot的数都集中到最左边 if (nums[i] <= pivot){ // Put numbers < pivot to pivot's left swap(nums, low, i); low++; } } swap(nums, low, right);// Finally, swap A[right] with A[low] if (low == k)// Found kth smallest number return nums[low]; else if (low < k)// Check right part return findKthLargest(nums, low + 1, right, k); else // Check left part return findKthLargest(nums, left, low - 1, k); } void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; }
相关文章推荐
- Leetcode 215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- <LeetCode OJ> 215. Kth Largest Element in an Array
- LeetCode 215. Kth Largest Element in an Array(第K大元素)
- Leetcode:215. Kth Largest Element in an Array (week 10)
- [LeetCode] 215. Kth Largest Element in an Array
- LeetCode-215. Kth Largest Element in an Array
- 【LeetCode】 215. Kth Largest Element in an Array
- [leetcode]215. Kth Largest Element in an Array@Java解题报告
- [勇者闯LeetCode] 215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array (Medium) (cpp)
- [Leetcode] 215. Kth Largest Element in an Array 解题报告
- leetcode215. Kth Largest Element in an Array
- LeetCode 215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- leetCode刷题归纳-Divide and Conquer(215. Kth Largest Element in an Array)
- Leetcode 215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- 【leetcode】215. Kth Largest Element in an Array
- LeetCode· 215. Kth Largest Element in an Array