215. Kth Largest Element in an Array

sort 之后再 index,或者借助 quick sort 的思路做 quick selection

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        sortedNums = sorted(nums, reverse=True)
        kth = float("-inf")
        n = 0
        while n < k:
            if sortedNums[n] != kth:
                kth = sortedNums[n]
            n += 1
        return kth

Last updated

Was this helpful?