-
Notifications
You must be signed in to change notification settings - Fork 17
/
busiest-time-in-the-mall.py
51 lines (36 loc) · 2.17 KB
/
busiest-time-in-the-mall.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
"""
Busiest Time in The Mall
The Westfield Mall management is trying to figure out what the busiest moment at the mall was last year. You’re given data extracted from the mall’s door detectors. Each data point is represented as an integer array whose size is 3. The values at indices 0, 1 and 2 are the timestamp, the count of visitors, and whether the visitors entered or exited the mall (0 for exit and 1 for entrance), respectively. Here’s an example of a data point: [ 1440084737, 4, 0 ].
Note that time is given in a Unix format called Epoch, which is a nonnegative integer holding the number of seconds that have elapsed since 00:00:00 UTC, Thursday, 1 January 1970.
Given an array, data, of data points, write a function findBusiestPeriod that returns the time at which the mall reached its busiest moment last year. The return value is the timestamp, e.g. 1480640292. Note that if there is more than one period with the same visitor peak, return the earliest one.
Assume that the array data is sorted in an ascending order by the timestamp. Explain your solution and analyze its time and space complexities.
Example:
input: data = [ [1487799425, 14, 1],
[1487799425, 4, 0],
[1487799425, 2, 0],
[1487800378, 10, 1],
[1487801478, 18, 0],
[1487801478, 18, 1],
[1487901013, 1, 0],
[1487901211, 7, 1],
[1487901211, 7, 0] ]
output: 1487800378 # since the increase in the number of people
# in the mall is the highest at that point
Constraints:
[time limit] 5000ms
[input] array.array.integer data
1 ≤ data.length ≤ 100
[output] integer
"""
def find_busiest_period(data):
max_timestamp, max_count = 0, 0
cur_timestamp, cur_count = 0, 0
for timestamp, count, enter in data:
if timestamp != cur_timestamp:
if cur_count > max_count:
max_timestamp, max_count = cur_timestamp, cur_count
cur_timestamp = timestamp
cur_count += count if enter else -count
if cur_count > max_count:
max_timestamp, max_count = cur_timestamp, cur_count
return max_timestamp