且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到最大间隔与python重叠的点的最有效方法

更新时间:2023-10-28 21:40:34

假设 ins outs 是登录和注销时间:

Let's say ins and outs are the log in and log out times:

ins = [4,0,1,7,2]
outs = [5,3,9,8,6]

在一个已排序的列表中将其组合在一起,并用数字的符号表示它是到达"(正)还是出发"(负):

Combine them in one sorted list with the sign of the number indicating whether it is an "arrival" (positive) or "departure" (negative):

times = sorted(ins + [-x for x in outs], key=abs)

现在,遍历列表并计算发生的到达"和离开":

Now, walk through the list and count the "arrivals" and "departures" as they happen:

lmax = -1
logged = 0
for t in times:
    if t >= 0:
        logged += 1
        if logged > lmax:
            tmax = t
            lmax = logged
    else:
        logged -= 1

print(tmax, lmax)
#2 3