-
Notifications
You must be signed in to change notification settings - Fork 1
/
kth-largest-element-in-an-array.py
51 lines (40 loc) · 1.35 KB
/
kth-largest-element-in-an-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
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.
Example 1:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Example 2:
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
Constraints:
1 <= k <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
"""
from typing import List
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# 第k大的数转换成第nums.length - k + 1小的数(因为是升序排列)
k = len(nums) - k + 1
left, right = 0, len(nums) - 1
while left <= right:
pivot = self.partition(nums, left, right)
if pivot == k - 1:
return nums[pivot]
elif pivot < k - 1:
left = pivot + 1
else:
right = pivot - 1
return nums[left]
def partition(self, nums, left, right):
import random
pivot = random.randint(left, right)
nums[pivot], nums[right] = nums[right], nums[pivot]
pivot = right
i = left
for j in range(left, right):
if nums[j] < nums[pivot]:
nums[i], nums[j] = nums[j], nums[i]
i += 1
nums[i], nums[pivot] = nums[pivot], nums[i]
return i