我正在尝试计算服务的每日最大访问量。 我有每个访客的会话开始时间和结束时间的数据。
搜索后,我从此参考中获得了线索。 这是获取最大客人人数的代码。
找到最大间隔重叠的点 考虑一个大型聚会,其中记录了访客进入和离开时间的日志记录。查找最大人数参加聚会的时间。请注意,寄存器中的条目没有任何顺序。
# Program to find maximum guest
# at any time in a party
def findMaxGuests(arrl, exit, n):
# Sort arrival and exit arrays
arrl.sort();
exit.sort();
# guests_in indicates number of
# guests at a time
guests_in = 1;
max_guests = 1;
time = arrl[0];
i = 1;
j = 0;
# Similar to merge in merge sort to
# process all events in sorted order
while (i < n and j < n):
# If next event in sorted order is
# arrival, increment count of guests
if (arrl[i] <= exit[j]):
guests_in = guests_in + 1;
# Update max_guests if needed
if(guests_in > max_guests):
max_guests = guests_in;
time = arrl[i];
# increment index of arrival array
i = i + 1;
else:
guests_in = guests_in - 1;
j = j + 1;
print("Maximum Number of Guests =",
max_guests, "at time", time)
# Driver Code
arrl = [1, 2, 10, 5, 5];
exit = [4, 5, 12, 9, 12];
n = len(arrl);
findMaxGuests(arrl, exit, n);
使用上面的代码,我可以一直获得最大数量。
Example :
Input: arrl[] = {1, 2, 9, 5, 5}
exit[] = {4, 5, 12, 9, 12}
First guest in array arrives at 1 and leaves at 4,
second guest arrives at 2 and leaves at 5, and so on.
Output: 5
There are maximum 3 guests at time 5.
但是,我每天需要最多访客。 我有一对像这样的数组(开始时间和结束时间)。
[['2018-09-03T10:03:05.424000000']
['2018-09-03T10:10:11.715000000']
['2018-09-03T10:10:28.251000000']
...
['2020-01-28T03:01:44.958000000']
['2020-01-28T03:00:56.210000000']
['2020-01-28T03:58:14.315000000']]
如何编辑此代码以获得每日最大数量?