Algorithm to find the maximum sum in a sequence of overlapping intervals
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In many computational problems, particularly those involving scheduling, genetic sequencing, or even gaming, sequences of overlapping intervals are a common occurrence. The challenge of finding a maximum sum from these overlapping intervals involves developing an algorithm that can efficiently process and compute the sums to produce an optimal result. This article presents a comprehensive look at one such algorithm, designed for this purpose.
Problem Definition
Given a set of intervals, each with a starting point, an endpoint, and a positive numeric value (representing some "weight"), the task is to find a subset of non-overlapping intervals that provides the maximum sum of weights.
Input and Output
• Input: A list of intervals. Each interval is represented as a tuple (start, end, value)
.
• Output: A subset of non-overlapping intervals with the highest possible sum of values.
Algorithm Overview
The typical approach to solving this problem is to leverage dynamic programming. The intervals must be sorted by their ending times to help facilitate a sequence that maximizes the value without overlap. Here is a step-by-step description of the algorithm:
- Sort Intervals: First, sort all intervals based on their end times. This sorting step ensures that when choosing an interval, all preceding intervals are candidates for forming a valid, non-overlapping sequence.
- Dynamic Programming Setup: Utilize a dynamic programming table (
dp[]), wheredp[i]represents the maximum sum of interval weights ending at intervali. - Populate the DP Table: For each interval, determine the maximum sum by considering: • Including the current interval and adding its value to the highest possible value from non-overlapping preceding intervals. • Excluding the current interval and taking the highest sum computed thus far.The computation for each
dp[i]is given by:Here,p(i)returns the index of the last interval that does not overlap with intervali. - Extract the Solution: The highest value in the
dp[]table after processing all intervals corresponds to the maximum achievable sum of non-overlapping intervals.
Example
Consider the intervals: [(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (5, 8, 11), (7, 9, 2)]
.
- Sort Intervals: Sorted intervals based on end times:
[(1, 3, 5), (2, 5, 6), (4, 6, 5), (6, 7, 4), (7, 9, 2), (5, 8, 11)]. - Process Intervals: Compute
dp[]as follows: •dp[0] = 5(Base case) •dp[1] = max(6 + dp[0], dp[0]) = max(6, 5) = 6•dp[2] = max(5 + dp[0], dp[1]) = max(10, 6) = 10•dp[3] = max(4 + dp[2], dp[2]) = max(14, 10) = 14•dp[4] = max(11 + dp[0], dp[3]) = max(16, 14) = 14•dp[5] = max(2 + dp[3], dp[4]) = max(16, 14) = 16 - Solution:
dp[5] = 16is the maximum sum attainable with these intervals.
Complexity
The time complexity of this algorithm is primarily due to sorting, which is , and populating the dp[]
table, which is . Thus, the overall complexity is . The space complexity is due to the dp[]
array.
Table of Key Points
| Step | Description |
| Input | List of intervals with start, end, and weight |
| Sorting | Sort intervals based on end times |
| DP Initialization | Initialize a list dp[] to store maximum sums at each step |
| DP Table Computation | Compute dp[i] using the formula: dp[i] = max(interval[i].value + dp[p(i)], dp[i-1]) |
| --- | --- |
| Time Complexity | |
| Space Complexity |
Additional Considerations
• Handling Edge Cases: Check for and handle cases where intervals have the same end time or overlap completely. Consider constraints like minimum and maximum lengths or values of intervals. • Parallel Computation: With proper adjustments, this algorithm could be parallelized for distributed computing environments to handle massive datasets.
The algorithm for finding the maximum sum in a sequence of overlapping intervals is not only elegant but also efficient, allowing for practical application in a variety of fields. By focusing on non-overlapping subsequences within sorted intervals, it takes a problem that could seem computationally intensive and solves it with a streamlined, logical approach.
Related reading
- Algorithm to find the minimum number of rectangles covering certain elements in a 2d array
- Algorithm to find the minimum value point of a function
- Algorithm to find the most common substrings in a string
- Algorithm to find the next number in a sequence
- Algorithm to find the total number of connected sets in a matrix
- Algorithm to find top 10 search terms
- Algorithm to find two points furthest away from each other
- Algorithm to find two repeated numbers in an array, without sorting

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.