Problem
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.
Example
1 | Input: [3,2,1,5,6,4] and k = 2 |
1 | Input: [3,2,3,1,2,4,5,5,6] and k = 4 |
Boundary Conditions
- What if k > array
- What if k < 0
- What if array is empty
Solution1
Method: Built-in
Time Complexity: O(n log n)
Space Complexity:
1 | class Solution: |
Solution2
Method: Divide and Conquer (recursive)
Time Complexity:
Space Complexity:
1 | class Solution: |