Time Intersection

Description

Give two users' ordered online time series, and each section records the user's login time point x and offline time point y. Find out the time periods when both users are online at the same time, and output in ascending order.you need return a list of intervals.

  • We guarantee that the length of online time series meet 1 <= len <= 1e6.
  • For a user's online time series, any two of its sections do not intersect.

Example

Example 1:

Input: seqA = [(1,2),(5,100)], seqB = [(1,6)]
Output: [(1,2),(5,6)]
Explanation: In these two time periods (1,2), (5,6), both users are online at the same time.

Example 2:

Input: seqA = [(1,2),(10,15)], seqB = [(3,5),(7,9)]
Output: []
Explanation: There is no time period, both users are online at the same time.
思路:扫描线
根据每个上线的时间段generate events(time,status)。将events按照时间排序,如果时间相同,下线优先于上线。
扫描排序后的events,如果发现上线event count++,如果count更新后 == 2,说明此时两人同时在线,记录此时的时间。 如果发现下线event count--,如果count更新后变成1,说明从两人同时在线的状态变成了一个人在线。生成这个时间段(prevTime,currTime)并加入结果中。

猜你喜欢

转载自www.cnblogs.com/FLAGyuri/p/12078134.html
今日推荐