确定给定日期范围之间是否存在时间重叠

给出了非常多的数据范围(开始和结束日期)。我试图找到最有效的方法来确定给定的(新)数据范围是否与其他任何范围重叠。有什么想法吗?

fql920011429 回答:确定给定日期范围之间是否存在时间重叠

这是一种固定时间的方法。在树中维护时间戳:年->月->日->小时->分钟->秒->您想要的细粒度。

要检查新的日期范围:

1. Traverse the tree,creating new nodes as needed,to add the start-of-range 
   timestamp.
2. Back up to the highest common node and retraverse the tree from there,again 
   creating new nodes as needed to add the end-of-range timestamp.
3. As you move between the two timestamps,check every node you pass for children 
   that live between the two timestamps. If you find any then there is overlap.

通过在树中存储更多信息,您可以识别特定的重叠范围(相对于存在范围),并产生重叠范围的数量。

简单的M / D示例:

范围1:1/19-3/19

范围2:2/19-4/19

在添加范围一之后,树具有三个节点:2019年,第1个月和第3个月,边缘为2019-> 1和2019-> 3。

我们通过添加2019-> 2处理范围2,然后回到2019,并注意到2019的孩子3生活在2到4之间。最后我们为第4个月添加节点。

本文链接:https://www.f2er.com/3131114.html

大家都在问