Skip to content

Commit e61e963

Browse files
committed
Solution for Kth Largest Element in an Array
1 parent e4ef80e commit e61e963

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kth_largest_element_in_an_array.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Find the kth largest element in an unsorted array.
2+
# Note that it is the kth largest element in the sorted order, not the kth distinct element.
3+
# Note:
4+
# You may assume k is always valid, 1 ≤ k ≤ array's length.
5+
"""
6+
>>> findKthLargest([3, 2, 1, 5, 6, 4], 2)
7+
5
8+
>>> findKthLargest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4)
9+
4
10+
>>> findKthLargest([2], 1)
11+
2
12+
"""
13+
from typing import List
14+
15+
16+
def findKthLargest(nums: List[int], k: int) -> int:
17+
return sorted(nums)[-k]

0 commit comments

Comments
 (0)