Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 438 Bytes

215.md

File metadata and controls

19 lines (17 loc) · 438 Bytes

215. Kth Largest Element in an Array

Solution 1 (time O(n*log(k)), space O(k))

class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        from heapq import heapify, heappushpop
        heap = nums[:k]
        heapify(heap)
        for num in nums[k:]:
            heappushpop(heap, num)
        return heap[0]