我有几个数据,包括事务的开始及其以DateTime格式结束。我想弄清楚有多少交易在同一时间运行。有没有算法可以解决我的问题?
答案 0 :(得分:2)
如果您有“开始”和“结束”的列表,并且您想知道并发打开的连接的最大数量(或者在每个点处打开了多少个连接),我将执行以下操作(在伪代码中) ):
示例:
Input:
Connection A opened at 1 closed at 3
Connection B opened at 2 closed at 6
Connection C opened at 4 closed at 7
Connection D opened at 5 closed at 8
Create the following structure:
Timestamp: 1 2 3 4 5 6 7 8
First array sorted: +1 +1 -1 +1 +1 -1 -1 -1
Second array: 1 2 1 2 3 2 1 0
Max open connections = 3
Number of connections open at timestamp 6 = 2
第二个数组计算每个时间戳中并发打开的连接数,计算方法如下(伪代码):
second_array[i] = sum(first_array[1..i])