Given an array nums, return the kth largest element in the array.
Input: nums = [1, 2, 3, 4, 5], k = 2
Output: 4
Input: nums = [-5, 4, 1, 2, -3], k = 5
Output: -5
Input: nums = [11, 9, 8, 7, 3, 1], k = 4
Striver and the team have worked tirelessly over the past few days to bring this to you. Please bear with us a little more as we finalize everything.
Striver and the team have worked tirelessly over the past few days to bring this to you. Please bear with us a little more as we finalize everything.
Q: Why use a min-heap for k-th largest instead of max-heap?
A: A min-heap of size k tracks the top k largest elements, making extraction O(1). A max-heap stores everything, requiring O(k log n) extraction.
Q: What changes for k-th largest in a BST instead of an array?
A: Perform reverse inorder traversal, counting elements until reaching k. Time Complexity: O(h + k) ≈ O(log n + k) for balanced BSTs.
Q: How does QuickSelect compare to Median of Medians for selection problems?
A: QuickSelect is O(n) on average but O(n²)` worst case. Median of Medians ensures O(n) worst case but is more complex to implement.