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
Conference Room Booking OptimizationAvailable Time Slots:Room A9AM-11AM: $500Room B10AM-1PM: $800Room C2PM-4PM: $400Room D3PM-5PM: $600Timeline Analysis:9AM10AM11AM1PM2PM5PMOptimal Strategy:1. Sort by end time: A, B, C, D2. DP: Best combination = A + C + D3. Total Revenue: $500 + $400 + $600 = $1500Result:Selected: [0, 2, 3]Non-overlapping โœ“Maximum Revenue โœ“Binary SearchDynamic Programming
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โด)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for tracking results

n
2n
โœ“ 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
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
28.5K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen