Skip to contentKEEP THE CRAFTTHE SANCTUARY

Kth Largest Element in an Array

Find the kth largest value by keeping only the k largest values in a min-heap.

The problem

Implement Solution.findKthLargest(nums, k). Return the kth largest value in nums, where equal values count as separate positions: in [3, 3, 1], the 1st and 2nd largest are both 3. Return an int. nums has 1 to 100,000 integers between -10,000 and 10,000, and 1 <= k <= len(nums).

Starter code

class Solution:
    def findKthLargest(self, nums, k):
        pass

Public examples

  1. nums = [3, 2, 1, 5, 6, 4], k = 2gives5
  2. nums = [7, -2, 4], k = 1gives7
  3. nums = [4, 1, 3], k = 3gives1

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.