TikTok OA 面试真题解析:会议室分配 / Livestream Availability

26次阅读
没有评论

TikTok is hosting a livestream event with popular creators. Each creator has submitted their preferred start and end times 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:

  • 0 <= start(i) < end(i) <= 10^6
  • 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

Follow: Now, TikTok changes minds and wants to fit all the creators to the livestream event. In that case, how many livestream rooms should they provide? Return the minimum number of livestream rooms.

Example 1:

Input: intervals = [[0,30],[5,10],[10,20]]
Output: 2

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: 1

Example 3:

Input: [[7,10],[2,4],[7,10],[2,4]]
Output: 2

Example 4:

Input: [[0,30],[15,45],[30,60]]
Output: 2

Example 5:

Input: [[0,30],[15,45],[45,60],[45,70]]
Output: 2

Example 5:

Input: [[0,45],[15,30],[45,60],[45,70]]
Output: 2

这题本质上是经典的区间重叠问题:先判断所有创作者能否在同一个直播间顺序参加,只要任意两个时间段重叠就无法全部安排在同一场次;如果题目进一步要求求最少需要多少个直播间,则要统计任意时刻同时进行的最大区间数。常见做法是先按开始时间排序,再用最小堆维护当前直播间的结束时间,遇到可以复用的房间就弹出并放回新的结束时间,最终堆的最大规模就是答案。题目给出的样例也体现了边界处理,例如 [0,30] 和 [30,60] 可以复用同一房间,而 [15,45] 会与前后多个区间产生重叠。

正文完
 0