LeetCode – Kth Largest Element in an Array (Java)
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.
Java Solution 1 - Sorting
public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length-k]; } |
Time is O(nlog(n)). The problem of this solution is that sorting all elements is not necessary and is a overkill for getting just one element.
Java Solution 2 - Heap
We can use a min heap to solve this problem. The heap stores the top k largest elements.The top of the heap is the Kth Largest element and all other elements are greater than the heap top. Whenever the size is greater than k, delete the min. Time complexity is O(nlog(k)). Space complexity is O(k) for storing the top k numbers.
public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> q = new PriorityQueue<Integer>(k); for(int i: nums){ q.offer(i); if(q.size()>k){ q.poll(); } } return q.peek(); } |
Time complexity of n*log(k) is an improvement to Solution 1. However, this solution requires O(k) space complexity and it is also maintained k-element heap.
Java Solution 3 - Quick Sort
This problem can also be solved by using a similar method like quicksort.
public int findKthLargest(int[] nums, int k) { if (k < 1 || nums == null) { return 0; } return getKth(nums.length - k +1, nums, 0, nums.length - 1); } public int getKth(int k, int[] nums, int start, int end) { int pivot = nums[end]; int left = start; int right = end; while (true) { while (nums[left] < pivot && left < right) { left++; } while (nums[right] >= pivot && right > left) { right--; } if (left == right) { break; } swap(nums, left, right); } swap(nums, left, end); if (k == left + 1) { return pivot; } else if (k < left + 1) { return getKth(k, nums, start, left - 1); } else { return getKth(k, nums, left + 1, end); } } public void swap(int[] nums, int n1, int n2) { int tmp = nums[n1]; nums[n1] = nums[n2]; nums[n2] = tmp; } |
Average case time is O(n), worst case time is O(n^2).
Reference:
http://www.cs.yale.edu/homes/aspnes/pinewiki/QuickSelect.html
<pre><code> String foo = "bar"; </code></pre>
-
Chaitanya GOPIREDDY
-
Bonsai
-
Chaitanya GOPIREDDY
-
John Smith
-
Matias SM
-
Hooman
-
Jay
-
Matias SM
-
Aaron Ke
-
肖永博
-
Riaz Ud Din
-
Jason Zhu