-
Notifications
You must be signed in to change notification settings - Fork 40
/
track_kth_largest_stream_of_nums.py
86 lines (73 loc) · 2.01 KB
/
track_kth_largest_stream_of_nums.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# Date: 2018-09-23
#
# Description:
# There is a running stream of numbers, keep track of kth largest number.
#
# Approach:
# Implement a min heap of size k as min heap always has smallest element at
# root index or kth largest(as there are k elements in min heap).
# When there is an element smaller than root, we can ignore them as they can't
# contribute to first k elements otherwise we replace that element with root
# and min heapfies again.
#
# Complexity:
# O(k) Space, O(logk) Time
def min_heapify(a, n, idx):
"""Min heapifies an array of size n w.r.t index idx."""
min_idx = idx
left = 2*idx + 1
right = 2*idx + 2
if left < n and a[left] < a[min_idx]:
min_idx = left
if right < n and a[right] < a[min_idx]:
min_idx = right
if min_idx != idx:
a[min_idx], a[idx] = a[idx], a[min_idx]
min_heapify(a, n, min_idx)
def build_heap(a, n):
"""Builds a heap of size n."""
for idx in range(n/2, -1, -1):
min_heapify(a, n, idx)
def main():
min_heap = []
k = input('Enter k: ')
while True:
n = input('Enter next number: ')
if len(min_heap) < k - 1:
print 'Kth largest is: None'
min_heap.append(n)
elif len(min_heap) == k - 1:
min_heap.append(n) # Now length of min_heap list is k
build_heap(min_heap, k)
print 'Kth largest is: %d' % min_heap[0]
else:
if min_heap[0] < n:
min_heap[0] = n
min_heapify(min_heap, k, 0)
else:
pass # Element is smaller than current kth largest, Ignore that
print 'Kth largest is: %d' % min_heap[0]
if __name__ == '__main__':
main()
# Output:
# ---------
# Enter k: 3
# Enter next number: 10
# Kth largest is: None
# Enter next number: 2
# Kth largest is: None
# Enter next number: 3
# Kth largest is: 2
# Enter next number: 1
# Kth largest is: 2
# Enter next number: 11
# Kth largest is: 3
# Enter next number: 14
# Kth largest is: 10
# Enter next number: 13
# Kth largest is: 11
# Enter next number: 0
# Kth largest is: 11
# Enter next number: 50
# Kth largest is: 13