![]() |
VOOZH | about |
Given an array interval[] , where each element represents the following three values
startTime – the time when the interval beginsendTime – the time when the interval endsvalue – the value associated with this intervalFind the maximum sum of values by selecting at most two intervals that do not overlap.
Example:
Input: interval[] = [[1, 3, 2], [4, 5, 2], [2, 4, 3]]
Output: 4
Explanation: Select interval 1 and 2 (as third interval is overlapping). Therefore, maximum value is 2 + 2 = 4.
Input: interval[] = [[1, 3, 2], [4, 5, 2], [1, 5, 5]]
Output: 5
Explanation: As intervals 1 and 2 are non-overlapping but their value will be 2 + 2 = 4. So, instead of 1 and 2, only 3 can be selected with a value of 5.
Table of Content
For each interval (or pair of intervals), we calculate the sum of their values only if they do not overlap and keep track of the maximum sum found so far. We also consider the case where a single interval alone provides the maximum value.
This problem can be solved with the help of a priority queue. To solve this problem, follow the below steps:
Below is the implementation of the above approach
5