-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_KthLargestElementInArray.py
More file actions
35 lines (27 loc) · 1.12 KB
/
01_KthLargestElementInArray.py
File metadata and controls
35 lines (27 loc) · 1.12 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
# Question link - https://leetcode.com/problems/kth-largest-element-in-an-array/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# # Kth largest element
# k = len(nums) - k
# # Recursice function for the quickSelect
# def quickSelect(l , r):
# # Take the pivot , p
# pivot , p = nums[r] , l
# for i in range(l , r):
# if nums[i] <= pivot:
# nums[p] , nums[i] = nums[i] , nums[p]
# p += 1
# # Swap the pth element
# nums[p] , nums[r] = nums[r] , nums[p]
# # Cases for quickSelect
# if p > k :
# return quickSelect(l,p - 1)
# elif p < k:
# return quickSelect(p + 1 , r)
# else:
# return nums[p]
# # Call the function for quickSelect
# return quickSelect(0, len(nums) - 1)
# For any other submission approch , we use sort()
nums.sort()
return nums[len(nums) - k]