Premium Playback Time
Spotify is constantly evaluating the impact of its premium features. One key metric is understanding how much time users spend engaging with premium content. This problem asks you to calculate the total duration a user spent listening during designated "premium content" windows, based on their listening history.
You are given two lists of time intervals:
userSessions: Represents a user's listening history. Each interval [start, end] indicates a period when the user was actively listening. These intervals are guaranteed to be sorted by start time and non-overlapping.premiumContentWindows: Represents periods when special premium content (e.g., ad-free listening, exclusive commentary) was available. These intervals may be overlapping and are not necessarily sorted.Your task is to implement a function calculatePremiumPlaybackTime that returns the total duration (in minutes) the user spent listening within any premium content window.
userSessions: An array of arrays of numbers, number[][]. Each inner array [start, end] represents a listening session. 0 <= start < end. Intervals are sorted by start time and are non-overlapping.premiumContentWindows: An array of arrays of numbers, number[][]. Each inner array [start, end] represents a premium content window. 0 <= start < end. These intervals may be overlapping and are not necessarily sorted.number, representing the total duration (sum of lengths of intersections) the user spent listening within premium content windows.[start, end] includes start and excludes end. The length is end - start.userSessions or premiumContentWindows are empty, the total premium playback time is 0.[10, 20] overlaps with two distinct premium windows [5, 15] and [18, 25], the total counted time would be (15-10) + (20-18) = 5 + 2 = 7 (assuming premiumContentWindows were merged into [[5,15], [18,25]]). This implies you should first merge premiumContentWindows to simplify the intersection logic.Examples
Example 1
Input:
userSessions = [[0, 5], [10, 15], [20, 25]], premiumContentWindows = [[3, 7], [12, 18]]
Output:
5
Note:
Merged premium windows: [[3, 7], [12, 18]].
Example 2
Input:
userSessions = [[1, 10]], premiumContentWindows = [[0, 3], [5, 8], [7, 12]]
Output:
7
Note:
Merged premium windows: [[0, 3], [5, 12]].
Example 3
Input:
userSessions = [[1, 5]], premiumContentWindows = [[6, 10]]
Output:
0
Note:
Merged premium windows: [[6, 10]].
Constraints
0 <= userSessions.length <= 10^40 <= premiumContentWindows.length <= 10^4userSessions[i].length == 2, premiumContentWindows[i].length == 20 <= start < end <= 10^9 for all intervals.userSessions are sorted by start time and non-overlapping.premiumContentWindows may be unsorted and overlapping.start >= end). Assume valid input as per constraints.O(N log N) or O(N + M) approaches.Loading editor...