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中最大值

 1 public class Solution {
 2     public int findKthLargest(int[] nums, int k) {
 3         if (nums == null || k > nums.length)
 4             return 0;
 5         int length = nums.length, index = length - k, result = 0;
 6         PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length,
 7                                                             new Comparator<Integer>(){
 8                                                                 public int compare(Integer a, Integer b) {
 9                                                                     return (a - b);
10                                                                 }
11                                                             });
12         for (int i = 0; i < length; i++)
13             pq.add(nums[i]);
14         while (index >= 0) {
15             result = pq.poll();
16             index--;
17         }
18         return result;
19     }
20 }

Improvement

 1 public class Solution {
 2     public int findKthLargest(int[] nums, int k) {
 3         if (nums == null || k > nums.length)
 4             return 0;
 5         int length = nums.length, index = k, result = 0;
 6         PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length, Collections.reverseOrder());
 7         for (int i = 0; i < length; i++)
 8             pq.add(nums[i]);
 9         while (index > 0) {
10             result = pq.poll();
11             index--;
12         }
13         return result;
14     }
15 }

 

Solution 2 -- Quick Select

Quick Sort in Java

Average Time O(n)

 1 '''
 2 a: [3, 2, 5, 1], 2
 3 output: 2
 4 '''
 5 
 6 def swap(a, index1, index2):
 7     if index1 >= index2:
 8         return
 9     tmp = a[index1]
10     a[index1] = a[index2]
11     a[index2] = tmp
12 
13 def partition(a, start, end):
14     ''' a[start:end]
15         pivot: a[end]
16         return: index of pivot
17     '''
18     pivot = a[end]
19     cur_big = start - 1
20     for i in range(start, end):
21         if a[i] >= pivot:
22             cur_big += 1
23             swap(a, cur_big, i)
24     cur_big += 1
25     swap(a, cur_big, end)
26     return cur_big
27 
28 
29 def quick_select(a, k):
30     k -= 1
31     length = len(a)
32     start = 0
33     end = length - 1
34     while start < end:
35         index = partition(a, start, end)
36         if index == k:
37             return a[index]
38         if index > k:
39             # Note: end != index
40             end = index - 1
41         else:
42             # Note: start != index
43             start = index + 1
44             k = k - index
45     return -1
46 
47 a = [3, 2, 5, 1]
48 k = 1
49 print(quick_select(a, k))

 

相关文章:

  • 2022-01-17
  • 2021-09-30
  • 2022-03-06
  • 2022-12-23
猜你喜欢
  • 2021-07-10
  • 2021-04-29
  • 2022-01-03
  • 2021-12-19
  • 2022-01-15
相关资源
相似解决方案