Kth Largest Element | Heaps (opens in new tab)
Problem Statement Given an integer array nums and an integer k, return the kth largest element in the array. Note: Not the kth distinct element. Brute Force Intuition In an interview, you can explain it like this: Sort the entire array in descending order and return the kth element. Complexity Time Complexity: O(N log N) Space Complexity: O(1) Brute Force Code Arrays.sort(nums); return nums[nums.length - k]; Moving Towards the Better Heap Approach Do we really need the entire sorted array? No...
Read the original article