Skip to content

Commit 37f0235

Browse files
authored
Create 215-Kth-Largest-Element-in-an-Array.py
1 parent 1c39ed3 commit 37f0235

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def findKthLargest(self, nums: List[int], k: int) -> int:
3+
nums.sort()
4+
return nums[len(nums) - k]
5+
6+
k = len(nums) - k
7+
def quickSelect(l, r):
8+
if l == r: return nums[l]
9+
10+
pivot, p = nums[r], l
11+
for i in range(l, r):
12+
if nums[i] <= pivot:
13+
nums[p], nums[i] = nums[i], nums[p]
14+
p += 1
15+
nums[p], nums[r] = nums[r], nums[p]
16+
17+
if p > k: return quickSelect(l, p - 1)
18+
elif p < k: return quickSelect(p + 1, r)
19+
else: return nums[p]
20+
return quickSelect(0, len(nums) - 1)

0 commit comments

Comments
 (0)