forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kth_largest_element_in_long_array.py
37 lines (27 loc) · 1.14 KB
/
kth_largest_element_in_long_array.py
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
import heapq
from kth_largest_in_array import find_kth_largest
from test_framework import generic_test
def find_kth_largest_unknown_length(stream, k):
candidates = []
for x in stream:
candidates.append(x)
if len(candidates) >= 2 * k - 1:
# Reorders elements about median with larger elements appearing
# before the median.
find_kth_largest(k, candidates)
# Resize to keep just the k largest elements seen so far.
del candidates[k:]
# Finds the k-th largest element in candidates.
find_kth_largest(k, candidates)
return candidates[k - 1]
# Pythonic solution that uses library method but costs O(nlogk) time.
def find_kth_largest_unknown_length_pythonic(stream, k):
return heapq.nlargest(k, stream)[-1]
def find_kth_largest_unknown_length_wrapper(stream, k):
return find_kth_largest_unknown_length(iter(stream), k)
if __name__ == '__main__':
exit(
generic_test.generic_test_main(
"kth_largest_element_in_long_array.py",
'kth_largest_element_in_long_array.tsv',
find_kth_largest_unknown_length_wrapper))