-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path215.py
45 lines (40 loc) · 1.02 KB
/
215.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
class Solution:
"""
1040 ms 14.71%
13.1 MB 100%
"""
def findKthLargest(self, nums, k):
i = 0
length = len(nums)
j = length - 1
pivot = -1
while i < j:
print(nums, i, j, pivot)
diff = length - pivot
if diff > k:
i = pivot + 1
elif diff < k:
j = pivot - 1
else:
return nums[pivot]
pivot = self.partition(nums, i, j)
return nums[i]
def partition(self, nums, i, j):
if i == j:
return i
pivot = nums[i]
l = i + 1
r = j
while 1:
while l <= r and nums[l] < pivot:
l += 1
while l <= r and nums[r] >= pivot:
r -= 1
if l <= r:
nums[l], nums[r] = nums[r], nums[l]
else:
break
nums[r], nums[i] = nums[i], nums[r]
return r
s = Solution()
print(s.findKthLargest([3,2,1,5,6,4], 2))