-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1985.py
More file actions
39 lines (23 loc) · 648 Bytes
/
1985.py
File metadata and controls
39 lines (23 loc) · 648 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
28
29
30
31
32
33
34
35
36
37
38
39
# leetcode problem no. 1985(Medium )
import heapq
from heapq import heapify
def kthLargestNumber(nums, k):
heapq.heapify(nums)
for i in range(k-1):
nums.pop()
return nums.pop()
# def kthLargestNumber(nums, k):
# n = len(nums)
# nums.sort()
# for i in range(k-1):
# nums.pop()
# return nums.pop()
nums = [3, 6, 7, 10]
k = 4 # fourth largest number == 3
print(kthLargestNumber(nums, k))
nums = [2, 21, 12, 1]
k = 3 # third largest number == 2
print(kthLargestNumber(nums, k))
# nums = [0, 0]
# k = 2 # second largest number == 0
# print(kthLargestNumber(nums, k))