Maximum Score of Non-overlapping Intervals - Problem
Imagine you're an event scheduler with a list of valuable events, each having a specific time window and importance score. Your goal is to select up to 4 non-overlapping events that maximize your total score while returning the lexicographically smallest indices.
Given a 2D array intervals where intervals[i] = [start_i, end_i, weight_i], each interval represents an event that:
- Starts at position
start_i - Ends at position
end_i - Has a value/weight of
weight_i
Key Rules:
- Two intervals are overlapping if they share ANY point (including boundaries)
- You can choose at most 4 intervals
- Return the indices of chosen intervals in lexicographically smallest order
- Maximize the sum of weights among all valid combinations
Input & Output
example_1.py โ Basic Case
$
Input:
intervals = [[1,3,2],[2,4,3],[3,6,1]]
โบ
Output:
[0, 2]
๐ก Note:
We can select intervals at indices 0 and 2: [1,3,2] and [3,6,1]. They don't overlap (interval 0 ends at 3, interval 2 starts at 3, but since they share the boundary point 3, they're considered overlapping in this problem). Actually, we should select [0,2] with intervals [1,3) and [3,6) which gives total weight 2+1=3. The optimal is actually [1] with weight 3, but among equal scores we want lexicographically smallest indices.
example_2.py โ Multiple Valid Combinations
$
Input:
intervals = [[1,2,1],[2,3,2],[3,4,3],[4,5,4]]
โบ
Output:
[0, 1, 2, 3]
๐ก Note:
All intervals are non-overlapping since each ends exactly when the next begins. However, the problem states that sharing boundary points makes intervals overlapping, so we can only select non-adjacent intervals. The optimal solution would be selecting indices that don't share boundaries.
example_3.py โ Single Interval
$
Input:
intervals = [[1,5,10]]
โบ
Output:
[0]
๐ก Note:
With only one interval available, we select it for a total weight of 10. The lexicographically smallest (and only) solution is [0].
Visualization
Tap to expand
Understanding the Visualization
1
Sort Meetings
Arrange all meeting requests by their end times to process them optimally
2
Find Compatible
For each meeting, quickly find the latest non-conflicting previous meeting using binary search
3
Build Solutions
Use dynamic programming to build up optimal solutions, deciding whether to include each meeting
4
Choose Best
Select the combination with maximum value, preferring lexicographically smallest indices for ties
Key Takeaway
๐ฏ Key Insight: By sorting intervals by end time and using dynamic programming with binary search, we can efficiently find the maximum-weight non-overlapping subset while ensuring lexicographically smallest indices among optimal solutions.
Time & Space Complexity
Time Complexity
O(nโด)
We need to check all combinations of up to 4 intervals, which in worst case is O(C(n,4)) = O(nโด)
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space for tracking results
โ Linear Space
Constraints
- 1 โค intervals.length โค 104
- intervals[i].length == 3
- 1 โค starti < endi โค 109
- 1 โค weighti โค 106
- At most 4 intervals can be selected
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code