Calculate Playback Duration within Window
At Netflix, optimizing content delivery and user experience is paramount. Imagine a scenario where a user has a curated list of content segments (e.g., scenes from a show, trailers, bonus features) they wish to watch. To provide a smooth experience, we need to calculate the actual unique viewing time within a specific, predefined playback window, accounting for potential overlaps between content segments and segments extending beyond the window.
Your task is to implement a function that takes a list of content segments and a single playback window. It should return the total unique duration of content that falls within the specified playback window. You must account for segments that might overlap with each other or extend partially/fully outside the given window.
contentSegments: An array of intervals, where each interval [start, end] represents a content segment. start and end are non-negative integers representing time points. The intervals are inclusive of start and exclusive of end (i.e., [start, end)).playbackWindow: A single interval [windowStart, windowEnd] representing the total time available for playback. windowStart and windowEnd are non-negative integers, inclusive of windowStart and exclusive of windowEnd.number representing the total unique duration of content segments that fall within the playbackWindow.start, end, windowStart, windowEnd) are non-negative integers.[a, b) implies a is included, b is excluded. The duration of [a, b) is b - a.contentSegments is empty or playbackWindow has zero or negative duration (windowStart >= windowEnd), the total duration should be 0.contentSegments are not necessarily sorted.Examples
Example 1
Input:
contentSegments = [[1, 5], [3, 7], [8, 10]], playbackWindow = [0, 10]
Output:
8
Note:
All segments are within the window. Merging [1,5] and [3,7] yields [1,7]. The effective segments are [[1,7], [8,10]]. Total duration is (7-1) + (10-8) = 6 + 2 = 8.
Example 2
Input:
contentSegments = [[0, 3], [5, 8], [7, 12]], playbackWindow = [2, 10]
Output:
6
Note:
[0,3] to [2,10] -> [2,3]. 2. Clip [5,8] to [2,10] -> [5,8]. 3. Clip [7,12] to [2,10] -> [7,10]. Resulting clipped segments: [[2,3], [5,8], [7,10]]. Merging [5,8] and [7,10] yields [5,10]. The effective segments are [[2,3], [5,10]]. Total duration is (3-2) + (10-5) = 1 + 5 = 6.Example 3
Input:
contentSegments = [[10, 20], [25, 30]], playbackWindow = [0, 5]
Output:
0
Note:
All contentSegments are entirely outside the playbackWindow. After clipping, there are no valid segments remaining. Total duration is 0.
Constraints
0 <= contentSegments.length <= 10^50 <= start <= end <= 10^9 for any [start, end] in contentSegments.0 <= windowStart <= windowEnd <= 10^9 for playbackWindow = [windowStart, windowEnd].number).Loading editor...