Description
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Example 2:
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
My Solution
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
import heapq
h = []
for val in nums:
heapq.heappush(h, -val)
for i in range(k):
answer = heapq.heappop(h)
return -answer
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
[프로그래머스] level1. 성격 유형 검사하기 (0) | 2022.09.05 |
---|---|
[프로그래머스] level1. 신고 결과 받기 (0) | 2022.09.05 |
[Leetcode] Medium : Corporate Flight Bookings (0) | 2021.11.15 |
[Leetcode] Medium : Sum of Square Numbers (0) | 2021.11.15 |
[Leetcode] Medium : Number of Provinces (0) | 2021.11.08 |