Kth Largest Element in an Array — C# Coding Problem
Difficulty: medium | Category: heap
Problem Description
Given an integer array `nums` and an integer `k`, return the `k`th largest element in the array. Note: it is the `k`th largest element in sorted order, not the `k`th distinct element. Hint: Use a min-heap of size `k`. For each element, push it onto the heap. If the heap size exceeds `k`, pop the minimum. The heap's minimum is the kth largest.
Examples
Example 1
Input: nums = [3, 2, 1, 5, 6, 4], k = 2
Output: 5
Example 2
Input: nums = [3, 2, 3, 1, 2, 4, 5, 5, 6], k = 4
Output: 4