- ์ต๋ํ ๊ฐ๋
์ฌ์ฉ (๋ง์ด๋์ค)
- element ๊ฐ์ ๋ฆฌํดํ๋ ๊ฑด๋ฐ ์๊ฐ index์ ๊ฐ๋
์ผ๋ก ์ฐฉ๊ฐํ์
- answer๋ฅผ +1์ฉ ์นด์ดํธํ๋ค๊ฐ ์กฐ๊ฑด๋ฌธ์ ๋ง๋ฌ์ ๋ ๋ฆฌํดํ๋ ํ์์ผ๋ก ํด์คฌ์๋๋ฐ ๊ทธ๋ ๊ฒ ํ๋ฉด ์ ๋์ง
Solution
import heapq
from typing import List
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
answer = 0
heap = []
for num in nums:
heapq.heappush(heap, -num)
for _ in range(k):
answer = -heapq.heappop(heap)
return answer
Reference
Leave a comment