거누의 개발노트

[파이썬] 215. Kth Largest Element in an Array - 리트코드 본문

코딩테스트

[파이썬] 215. Kth Largest Element in an Array - 리트코드

Gogozzi 2022. 5. 25. 19:40
반응형

문제

정수 배열 nums이 주어지면 배열에서 k번째로 가장 큰 요소를 반환 하는 프로그램을 작성하시오.

입력

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

출력

Output: 5

작성한 코드1

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        return heapq.nlargest(k,nums)[-1]

 

 

https://leetcode.com/problems/kth-largest-element-in-an-array/

 

Kth Largest Element in an Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

반응형
Comments