문제 링크 : https://leetcode.com/problems/kth-largest-element-in-an-array/
kth largest element를 반환하는 문제이다.
문제 난이도는 medium인데 생각보다 어렵지는 않게 풀었다
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort(reverse = True)
return nums[k-1]
먼저 sort를 이용하여 정렬한다
이때 큰 순서대로 내림차순으로 정렬하기 위해서는 reverse = True를 이용한다.
그리고 k번째로 큰 인덱스를 반환하기 위해 k-1 에 있는 인덱스를 반환한다.
(인덱스는 0부터 시작하기 때문에)
Runtime: 109 ms, faster than 40.93% of Python3 online submissions for Kth Largest Element in an Array.
Memory Usage: 14.7 MB, less than 76.53% of Python3 online submissions for Kth Largest Element in an Array.