TikTok 面试真题解析:区间是否冲突(Can Attend All Intervals)|重叠判断基础题

32次阅读
没有评论

TikTok is hosting a livestream event with popular creators. Each creator has submitted their preferred start time and end time for their part in the livestream. Only one creator can livestream at the event at a time.

Given an array of available time intervals where intervals[i] = [start(i), end(i)], determine if all creators could attend all.

Constraints:

  1. 0 <= start(i) < end(i) <= 10^6
  2. Length of each interval will be 2
Example 1:Input:
intervals = [[0,30],[5,10],[15,20]]Output:
false
Example 2:Input:
intervals = [[7,10],[2,4]]Output:
true

这道题本质是判断区间是否有重叠。常见做法是先按照开始时间排序,然后依次比较当前区间的开始时间是否早于前一个区间的结束时间。如果出现这种情况,就说明存在冲突;如果遍历过程中都没有冲突,则可以全部安排。难点在于排序后如何正确处理边界条件。

正文完
 0