Published:
Updated:

  • ์ตœ๋Œ€ํž™ ๊ฐœ๋… ์‚ฌ์šฉ (๋งˆ์ด๋„ˆ์Šค)
  • 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