Checking for overlap between time spans

后端 未结 5 2188
别跟我提以往
别跟我提以往 2021-01-06 01:23

I have a list of time entries (HHMM format) with a start time and a stop. I\'m having trouble figuring out how to code it in Python where it returns if there\'s an overlap o

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 01:52

    For future reference, the solution of @Roy doesn't work for intervals that have the same end or start times. The following solution does:

    intervals = [[100, 200], [150, 250], [300, 400], [250, 500], [100, 150], [175, 250]]
    intervals.sort()
    l = len(intervals)
    overlaps = []
    for i in xrange(l):
      for j in xrange(i+1, l):
        x = intervals[i]
        y = intervals[j]
        if x[0] == y[0]:
          overlaps.append([x, y])
        elif x[1] == y[1]:
          overlaps.append([x, y])
        elif (x[1]>y[0] and x[0]

    Also, an Interval Tree could be used for these kinds of problems.

提交回复
热议问题