알고리즘 with python/알고리즘 스터디
LeetCode 215. Kth Largest Element in an Array with Python
정민됴
2024. 1. 5. 10:46
https://leetcode.com/problems/kth-largest-element-in-an-array/
import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums = [-1 * num for num in nums]
heapq.heapify(nums)
answer = 0
for _ in range(k):
answer = heapq.heappop(nums)
return answer * -1
최대힙처럼 작동해야했기 때문에 -1을 곱했다.
sort로 풀어도 문제없는 문제였다.