间隔列表中最多两个不重叠间隔的最大总和 |间隔调度问题
给定一个长度为N的数组区间,其中每个元素代表三个值,即{startTime, endTime, value} 。任务是找到最多两个不重叠区间的值的最大总和。
例子:
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.
方法:这个问题可以借助优先级队列来解决。要解决此问题,请执行以下步骤:
- 对给定的数组间隔wrt startTime 进行排序。如果两个区间的startTime相同,则根据endTime对其进行排序。
- 将{endTime, value}对存储在优先级队列中,并根据endTime进行排序。
- 遍历给定的数组,计算所有endTime小于当前区间startTime的事件的最大值,并将其存储在变量max 中。
- 现在,在每次遍历之后更新 ans ,ans= Math.max(ans, max + interval[i][2]) 。
- 返回 ans 作为这个问题的最终答案。
下面是上述方法的实现
C++
// C++ algorithm for above approach
#include
using namespace std;
// Function to find
// maximum value of atmost two
// non-overlapping intervals
int maxTwoNonOverLapping(vector >& interval)
{
// Sorting the given array
// on the basis of startTime
sort(interval.begin(), interval.end(),
[](vector& a, vector& b) {
return (a[0] == b[0]) ? a[1] < b[1]
: a[0] < b[0];
});
// Initializing Priority Queue
// which stores endTime
// and value and sort
// on the basis of endTime
priority_queue > pq;
// Initializing max
// and ans variables
int ma = 0;
int ans = 0;
// Traversing the given array
for (auto e : interval) {
while (!pq.empty()) {
// If endTime from priority
// queue is greater
// than startTime of
// traversing interval
// then break the loop
if (pq.top()[0] >= e[0])
break;
vector qu = pq.top();
pq.pop();
// Updating max variable
ma = max(ma, qu[1]);
}
// Updating ans variable
ans = max(ans, ma + e[2]);
pq.push({ e[1], e[2] });
}
// Returning ans
return ans;
}
// Driver Code
int main()
{
vector > interval
= { { 1, 3, 2 }, { 4, 5, 2 }, { 1, 5, 5 } };
int maxValue = maxTwoNonOverLapping(interval);
cout << maxValue;
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java algorithm for above approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String[] args)
{
int[][] interval
= { { 1, 3, 2 }, { 4, 5, 2 }, { 1, 5, 5 } };
int maxValue = maxTwoNonOverLapping(interval);
System.out.println(maxValue);
}
// Function to find
// maximum value of atmost two
// non-overlapping intervals
public static int maxTwoNonOverLapping(int[][] interval)
{
// Sorting the given array
// on the basis of startTime
Arrays.sort(interval,
(a, b)
-> (a[0] == b[0]) ? a[1] - b[1]
: a[0] - b[0]);
// Initializing Priority Queue
// which stores endTime
// and value and sort
// on the basis of endTime
PriorityQueue pq
= new PriorityQueue<>((a, b) -> a[0] - b[0]);
// Initializing max
// and ans variables
int max = 0;
int ans = 0;
// Traversing the given array
for (int[] e : interval) {
while (!pq.isEmpty()) {
// If endTime from priority
// queue is greater
// than startTime of
// traversing interval
// then break the loop
if (pq.peek()[0] >= e[0])
break;
int[] qu = pq.remove();
// Updating max variable
max = Math.max(max, qu[1]);
}
// Updating ans variable
ans = Math.max(ans, max + e[2]);
pq.add(new int[] { e[1], e[2] });
}
// Returning ans
return ans;
}
}
输出
5
时间复杂度: O(NlogN)
辅助空间: O(N)