사용자 도구

사이트 도구


ps:problems:leetcode:215

Kth Largest Element in an Array

ps
링크leetcode.com/…
출처LeetCode
문제 번호215
문제명Kth Largest Element in an Array
레벨Medium
분류

선택 알고리즘

시간복잡도O(n)
인풋사이즈n<=???
사용한 언어Python
해결날짜2020/12/20

풀이

  • 출제 의도는 당연히 O(n)의 선택 알고리즘이겠지만, Python의 경우는 built-in sort함수를 쓰는 것이 실질적으로 더 빠르다.. (선택 알고리즘 참고)

코드

"""Solution code for "LeetCode 215. Kth Largest Element in an Array".

- Problem link: https://leetcode.com/problems/kth-largest-element-in-an-array/
- Solution link: http://www.teferi.net/ps/problems/leetcode/215
"""


class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:        
        return sorted(nums)[-k]

토론

댓글을 입력하세요:
O V Q᠎ T᠎ Z
 
ps/problems/leetcode/215.txt · 마지막으로 수정됨: 2020/12/20 15:00 저자 teferi