Data Structures19 sections · 729 units
Open in Course

Problem - Kth Largest Element

Classic heap problem

Given an integer array numsnums and an integer kk, return the k-th largest element. Note: it's the k-th largest in sorted order, not the k-th distinct element. Example: nums=[3,2,1,5,6,4]nums = [3,2,1,5,6,4], k=2k = 2 Output: 55 (sorted: [6,5,4,3,2,1][6,5,4,3,2,1], 2nd largest is 55) You're solving the textbook heap problem for k-th element selection.