-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_171.py
50 lines (41 loc) · 1.46 KB
/
problem_171.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
import sys
ENTER = "enter"
EXIT = "exit"
def get_busiest_slot(events):
ts_entries, ts_exits = dict(), dict()
max_time, min_time = -sys.maxsize, sys.maxsize
for event in events:
ts_dict = None
timestamp = event["timestamp"]
if event["type"] == ENTER:
ts_dict = ts_entries
else:
ts_dict = ts_exits
ts_dict[timestamp] = event["count"]
if timestamp < min_time:
min_time = timestamp
elif timestamp > max_time:
max_time = timestamp
people_inside = 0
max_people_inside = 0
start_time, end_time = None, None
for timestamp in range(min_time, max_time + 1):
if timestamp in ts_entries:
people_inside += ts_entries[timestamp]
if people_inside > max_people_inside:
max_people_inside = people_inside
start_time = timestamp
if timestamp in ts_exits:
if people_inside == max_people_inside:
end_time = timestamp
people_inside -= ts_exits[timestamp]
return (start_time, end_time)
# Tests
events = [
{"timestamp": 1526579928, "count": 3, "type": "enter"},
{"timestamp": 1526579982, "count": 4, "type": "enter"},
{"timestamp": 1526580054, "count": 5, "type": "exit"},
{"timestamp": 1526580128, "count": 1, "type": "enter"},
{"timestamp": 1526580382, "count": 3, "type": "exit"}
]
assert get_busiest_slot(events) == (1526579982, 1526580054)