#215
Kth Largest Element in an Array
medium· Heap / Priority Queueruns: 0Given an integer array nums and an integer k, return the kth largest element in the array. The kth largest element is the value at the kth position in the sorted (descending) array, not the kth distinct value.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 262
import heapq
class Solution:
def findKthLargest(self, nums: list[int], k: int) -> int:
heap = []
for num in nums:
heapq.heappush(heap, num)
if len(heap) > k:
heapq.heappop(heap)
return heap[0]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.