Time: N/A (0%), Space: N/A (0%) - LeetHub

This commit is contained in:
Deven
2025-11-26 20:01:46 -05:00
parent 16adc41a13
commit 5f2c51e273
@@ -0,0 +1,18 @@
import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
heap = nums[:k]
# Heapify (min) the first k elements
heapq.heapify(heap)
# iterate over k+1...n - O(n) times
# - if larger than min in heap, replace min - O(log k)
# - else do nothing
for num in nums:
if num > heap[0]:
heapq.heapreplace(heap, num)
# return min in heap
return heap[0]