관리 메뉴

솜씨좋은장씨

[leetCode] 215. Kth Largest Element in an Array (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 215. Kth Largest Element in an Array (Python)

솜씨좋은장씨 2020. 10. 19. 23:42
728x90
반응형

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

 

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.

 

Solution

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        sorted_list = sorted(nums, reverse=True)
        
        return sorted_list[k-1]

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments