215 Kth Largest Element in an Array
from queue import PriorityQueue
class Solution:
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
pq = PriorityQueue()
for i, num in enumerate(nums):
if pq.qsize() == k and pq.queue[0] < num:
pq.get()
pq.put(num)
elif pq.qsize() < k:
pq.put(num)
return pq.queue[0]Last updated