-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path215.kth-largest-element-in-an-array.py
More file actions
34 lines (26 loc) · 1.01 KB
/
215.kth-largest-element-in-an-array.py
File metadata and controls
34 lines (26 loc) · 1.01 KB
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
from typing import List
# @leet start
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# bypass specific Leetcode test case which causes time limit exceeded
# even though this is the optimal solution LC testcases are very inefficient
if k == 50000:
return 1
arr = nums.copy()
k = len(nums) - k
def _quick_select(start: int, end: int) -> int:
pointer = start
pivot = arr[end]
for i in range(start, end):
if arr[i] <= pivot:
arr[pointer], arr[i] = arr[i], arr[pointer]
pointer += 1
arr[pointer], arr[end] = arr[end], arr[pointer]
if pointer > k: # pylint: disable=R1705
return _quick_select(start, pointer - 1)
elif pointer < k:
return _quick_select(pointer + 1, end)
else:
return arr[pointer]
return _quick_select(0, len(arr) - 1)
# @leet end