Given an array of integers nums and a k with 1 <= k <= nums.length, return the k-th largest value. Duplicates count separately, so k = 1 is the maximum.
Kth Largest Value (Set 5) — the data. This set runs 4 cases: nums holds arrays of 24–43 items, with values from -30 to 59; k ranges over 1–2. The hidden cases are the largest of the set.
Example 1
kthLargestValue()
Input
nums =[50,18,-3,12,36,26,1,39,57,15,-12,29,53,11,-19,-24,51,43,32,-29,-3,55,6,0], k =2
Output
55
Why
`kthLargestValue` returns `55`forthisinput.
Example 2
kthLargestValue()
Input
nums =[-9,59,-30,-23,41,-30,-23,35,-24,21,18,41,22,3,51,56,-25,-18,15,53,17,18,-20,-8,-8,-23,12,19,30,-1,-9,15,19,53,52,6,-14,41,16,49,25,26], k =2
Output
56
Why
`kthLargestValue` returns `56`forthisinput.
Constraints
1<= k <=nums.length<=10^4-10^4<= nums[i]<=10^4
Every caseinthisset must pass: the first two are visible on Run, the last two unlock on Submit.
Topics
ArraysSortingHeap
Implement kthLargestValue in the editor and return the answer — printing it is never graded.