Maximum Score of Non-overlapping Intervals - Solution & Explanation
Problem Statement
You are given a 2D integer array intervals, where intervals[i] = [li, ri, weighti]. Interval i starts at position li and ends at ri, and has a weight of weighti. You can choose up to 4 non-overlapping intervals. The score of the chosen intervals is defined as the total sum of their weights.
Return the lexicographically smallest array of at most 4 indices from intervals with maximum score, representing your choice of non-overlapping intervals.
Two intervals are said to be non-overlapping if they do not share any points. In particular, intervals sharing a left or right boundary are considered overlapping.
Example 1:
Input: intervals = [[1,3,2],[4,5,2],[1,5,5],[6,9,3],[6,7,1],[8,9,1]]
Output: [2,3]
Explanation:
You can choose the intervals with indices 2, and 3 with respective weights of 5, and 3.
Example 2:
Input: intervals = [[5,8,1],[6,7,7],[4,7,3],[9,10,6],[7,8,2],[11,14,3],[3,5,5]]
Output: [1,3,5,6]
Explanation:
You can choose the intervals with indices 1, 3, 5, and 6 with respective weights of 7, 6, 3, and 5.
Constraints:
1 <= intevals.length <= 5 * 104intervals[i].length == 3intervals[i] = [li, ri, weighti]1 <= li <= ri <= 1091 <= weighti <= 109
Approach Overview
Problem Overview: You receive a list of intervals where each interval has a start time, end time, and score. The goal is to select a subset of non-overlapping intervals that maximizes the total score. If two intervals overlap in time, you can only choose one.
Approach 1: Brute Force Subset Enumeration (O(2^n) time, O(n) space)
The simplest idea is to generate every possible subset of intervals and check whether the chosen intervals overlap. For each valid subset, compute the total score and keep the maximum. Checking overlap requires sorting the chosen intervals and verifying adjacent ranges. This approach demonstrates the core constraint but becomes infeasible even for moderate n because the number of subsets grows exponentially.
Approach 2: Dynamic Programming with Sorting (O(n^2) time, O(n) space)
First sort intervals by their ending time. Define dp[i] as the maximum score achievable considering intervals up to index i. For each interval, iterate backward to find the latest interval that does not overlap. The recurrence becomes dp[i] = max(dp[i-1], score[i] + dp[prev]), where prev is the last compatible interval. This reduces the exponential search to polynomial time by reusing results. The idea mirrors classic weighted interval scheduling using dynamic programming.
Approach 3: DP + Binary Search (O(n log n) time, O(n) space)
Instead of scanning backward to locate the previous non-overlapping interval, store the sorted end times and use binary search. For each interval i, binary search the rightmost interval whose end time is less than or equal to the current start time. Combine its DP value with the current score. The recurrence remains the same, but the lookup becomes O(log n) rather than O(n). Sorting the intervals first using sorting ensures the DP state transitions are valid.
This approach scales well for large inputs. Each interval contributes a constant DP update and a binary search lookup.
Recommended for interviews: The DP with binary search solution is the expected approach. Starting from the brute force idea shows you understand the overlap constraint. Moving to sorted intervals and DP demonstrates the classic weighted scheduling optimization. Using binary search to locate the previous compatible interval reduces the complexity to O(n log n), which is the standard optimal solution interviewers look for.
Solution
Thinking
We pick at most four non-overlapping weighted intervals to maximize the total weight, breaking ties by the lexicographically smallest index tuple.
n\le 5times 10^4forbids subset search.This is weighted interval scheduling with a cap of four. After sorting by left endpoint, the next non-overlapping interval is a binary search.
State
(i,k)starts at intervaliwithkpicks remaining. We either skipior take it and jump tonxt[i], comparing both weight and the index list so the lexicographically smallest optimum is kept.
Copy the intervals and record each original index, then sort by left endpoint. For each interval i, binary-search the first position nxt[i] whose left endpoint is strictly greater than i's right endpoint (shared endpoints count as overlap).
Let f[i][k] be the maximum weight obtainable from interval i onward with at most k picks, and let g[i][k] store the corresponding lexicographically smallest index list. Transition from the back: skipping i inherits f[i+1][k]; taking i inserts its original index into g[nxt[i]][k-1] and adds the current weight. Keep the larger weight, or the lexicographically smaller index list on a tie. The answer is g[0][4].
The time complexity is O(n times log n) and the space complexity is O(n). At most 4 intervals are chosen, so inserting and comparing index lists is constant time.
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subset Enumeration | O(2^n) | O(n) | Conceptual understanding or very small input sizes |
| DP with Backward Scan | O(n^2) | O(n) | Moderate input sizes where implementation simplicity matters |
| DP + Binary Search (Weighted Interval Scheduling) | O(n log n) | O(n) | Optimal solution for large datasets and typical interview expectation |
Video Solution
3414. Maximum Score of Non-overlapping Intervals (Leetcode Hard) • Programming Live with Larry • 679 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Maximum Score of Non-overlapping Intervals easy or hard?
Maximum Score of Non-overlapping Intervals Python/Java solution
How to solve Maximum Score of Non-overlapping Intervals in O(n log n)?
What is the best approach for Maximum Score of Non-overlapping Intervals?
Is Maximum Score of Non-overlapping Intervals asked at Google/Amazon/Meta?
What data structure is used in Maximum Score of Non-overlapping Intervals?
What is the time complexity of Maximum Score of Non-overlapping Intervals?
Ready to solve this problem?
Practice Maximum Score of Non-overlapping Intervals with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor