4068. Maximize Meeting Earnings with Idle Gaps
DifficultyHard
Description
You are given a 2D integer array meetings, where meetings[i] = [starti, endi, revenuei] represents a meeting starting at time starti, ending at time endi, with revenue revenuei.
All meetings use half-open intervals [start, end), so meetings that only touch at endpoints do not overlap.
You may select any non-empty subset of meetings such that no two selected meetings overlap. You earn the revenue of each selected meeting.
Arrange the selected meetings in increasing order of their start times. For each pair of adjacent meetings in this order, you also earn 1 unit of revenue per unit of idle time between them. This idle time equals the later meeting's start time minus the earlier meeting's end time.
No idle revenue is earned before the earliest selected meeting starts or after the latest selected meeting ends. If only one meeting is selected, no idle revenue is earned.
Return the maximum total earnings achievable.
A subset of an array is a selection of elements of the array.
Example 1:
Input: meetings = [[2,5,4],[6,8,3]]
Output: 8
Explanation:
- Select both meetings. They do not overlap and earn
4 + 3 = 7units of meeting revenue. - The first meeting ends at time 5, and the second starts at time 6. This idle gap earns
6 - 5 = 1additional unit. - The maximum total earnings are
7 + 1 = 8.
Example 2:
Input: meetings = [[3,5,4],[4,7,8],[8,10,3]]
Output: 12
Explanation:
- Select the meetings at indices 1 and 2. They do not overlap and earn
8 + 3 = 11units of meeting revenue. - In chronological order, these meetings run from time 4 to 7 and from time 8 to 10. The idle gap earns
8 - 7 = 1additional unit. - The maximum total earnings are
11 + 1 = 12.
Example 3:
Input: meetings = [[1,2,2],[4,5,2],[7,9,3]]
Output: 11
Explanation:
- Select all three meetings. They do not overlap and earn
2 + 2 + 3 = 7units of meeting revenue. - The idle gap from time 2 to 4 earns
4 - 2 = 2additional units. - The idle gap from time 5 to 7 earns
7 - 5 = 2additional units. - The maximum total earnings are
7 + 2 + 2 = 11.
Constraints
1 <= meetings.length <= 105meetings[i] = [starti, endi, revenuei]0 <= starti < endi <= 1091 <= revenuei <= 109
Solutions
Solution 1: Sort, Binary Search, and DP
Thinking
There can be \(10^5\) meetings, so enumerating subsets does not work. Earnings come from two parts: the revenue of each selected meeting, and the idle gap between meetings that are adjacent after sorting by start time. A single meeting earns no idle revenue.
Fix one meeting as the last meeting of a schedule. Every meeting placed before it must end no later than its start, and the new gap is \(\textit{start}\) minus that previous end. The quantity to maximize is therefore the best earnings of a schedule ending at a meeting, minus that meeting's end time.
After sorting by end time, the meetings that can precede the current one form a prefix, and the prefix maximum can be located by binary search. Revenues and gaps are accumulated in \(64\)-bit integers.
Sort the meetings by ascending end time. Let \(f[i]\) be the maximum earnings of a schedule whose last meeting is meeting \(i\). Every non-empty schedule has a meeting that ends last, so the answer is the maximum of all \(f[i]\).
Selecting meeting \(i\) alone gives \(f[i]=\textit{revenue}_i\). If a meeting \(j\) with \(\textit{end}_j\le\textit{start}_i\) is placed before it, then
which rearranges to
After the sort, those indices \(j\) form a prefix. Let
using a sentinel while no meeting has been processed. When handling meeting \(i\), binary-search the first index \(p\) in \([0,i)\) whose end time is greater than \(\textit{start}_i\). Then \(\textit{preMax}[p]\) is the maximum above. If \(\textit{start}_i\) is smaller than the earliest end time, the prefix is empty and meeting \(i\) must be taken alone.
Then set \(\textit{preMax}[i+1]=\max(\textit{preMax}[i], f[i]-\textit{end}_i)\). Two meetings with the same end time overlap, so the binary search does not chain them.
The time complexity is \(O(n\log n)\) and the space complexity is \(O(n)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |