nathanwailes

Find kth largest (using heapq)

Jun 10th, 2024
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.19 KB | None | 0 0
  1. """
  2. """
  3. import heapq
  4.  
  5. def find_kth_largest(nums, k):
  6.     return heapq.nlargest(k, nums)[-1]
  7.  
  8. # Example usage
  9. nums = [3, 2, 1, 5, 6, 4]
  10. k = 2
  11. print(find_kth_largest(nums, k))  # Output: 5
  12.  
Advertisement
Add Comment
Please, Sign In to add comment