반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 215 | Kth Largest Element in an Array | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/kth-largest-element-in-an-array/
문제
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.
조건
- 1 <= k <= nums.length <= $10^4$
- $-10^4$ <= nums[i] <= $10^4$
내 풀이
reverse=True를 사요요해서 내림차순으로 정렬한다.
Python의 index는 0부터 시작하므로 k-1번째 원소를 리턴한다.
코드
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums, reverse=True)[k-1]
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1689 | Partitioning Into Minimum Number of Deci-Binary Numbers | Python (0) | 2022.06.27 |
---|---|
[630] 630 | Course Schedule III | Python (0) | 2022.06.23 |
[LeetCode] 820 | Short Encoding of Words | Python (0) | 2022.06.20 |
[LeetCode] 5 | Longest Palindromic Substring | Python (0) | 2022.06.16 |
[LeetCode] 1048 | Longest String Chain | Python (0) | 2022.06.15 |
Comments