-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathfraudulent-activity-notifications.py
50 lines (38 loc) · 1.17 KB
/
fraudulent-activity-notifications.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
#!/bin/python3
import sys
import bisect as bs
def index(a, x):
'Locate the leftmost value exactly equal to x'
i = bs.bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError
def median(a_sorted, days):
half = len(a_sorted)//2
if days % 2:
median = a_sorted[half]
else:
median = (a_sorted[half-1] + a_sorted[half])/2
return float(median)
def activityNotifications(log, days):
heap = sorted(log[:days])
res = 0
med = 0
to_del = 0
for ind in range(days, len(log)):
med = median(heap, days)
#print("heap: {}".format(heap))
#print("log[{}] = {} med = {}".format(ind, log[ind], med))
if float(log[ind]) >= 2*med:
res += 1
#del heap[heap.index(log[to_del])]
del heap[index(heap, log[to_del])]
bs.insort(heap, log[ind])
to_del += 1
return res
if __name__ == "__main__":
n, d = input().strip().split(' ')
n, d = [int(n), int(d)]
expenditure = list(map(int, input().strip().split(' ')))
result = activityNotifications(expenditure, d)
print(result)