-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path215_2.py
More file actions
27 lines (22 loc) · 736 Bytes
/
Copy path215_2.py
File metadata and controls
27 lines (22 loc) · 736 Bytes
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
class Solution(object):
def findKthLargest(self, nums, k):
return self.findKthSmallest(nums, len(nums) - k + 1)
def findKthSmallest(self, nums, k):
if not nums: return None
pos = self.partition(nums, 0, len(nums) - 1)
if k > pos + 1:
return self.findKthSmallest(nums[pos + 1:], k - pos - 1)
elif k < pos + 1:
return self.findKthSmallest(nums[:pos], k)
else:
return nums[pos]
def partition(self, nums, left, right):
pivot = nums[left]
while left < right:
while left < right and nums[right] >= pivot:
right -= 1
nums[left], nums[right] = nums[right], nums[left]
while left < right and nums[left] <= pivot:
left += 1
nums[left], nums[right] = nums[right], nums[left]
return left