Maximize Meeting Earnings with Idle Gaps Problem
Description
LeetCode Problem 4068.
You are given a 2D integer array meetings, where meetings[i] = [start_i, end_i, revenue_i] represents a meeting starting at time start_i, ending at time end_i, with revenue revenue_i.
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.
Example 1:
Input: meetings = [[2,5,4],[6,8,3]]
Output: 8
Explanation:
- Select both meetings. They do not overlap and earn 4 + 3 = 7 units of meeting revenue.
- The first meeting ends at time 5, and the second starts at time 6. This idle gap earns 6 - 5 = 1 additional 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 = 11 units 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 = 1 additional 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 = 7 units of meeting revenue.
- The idle gap from time 2 to 4 earns 4 - 2 = 2 additional units.
- The idle gap from time 5 to 7 earns 7 - 5 = 2 additional units.
- The maximum total earnings are 7 + 2 + 2 = 11.
Constraints
1 <= meetings.length <= 10^5meetings[i] = [start_i, end_i, revenue_i]0 <= start_i < end_i <= 10^91 <= revenue_i <= 10^9
Solution
Approach: Sort by End Time with a Prefix Maximum
The idle time between two chosen meetings is start_next - end_prev, so each chosen meeting except the first contributes +start and each chosen meeting except the last contributes -end.
Sort the meetings by end time. Let val for meeting i be the best total of a selection that ends with meeting i, counting its own revenue and the +start/-end terms of every earlier link, but not yet the -end of meeting i itself. Then:
- alone:
val = revenue; - after an earlier meeting
jthat ends no later thanstart_i:val = revenue_i + start_i + (val_j - end_j).
The meetings that end by start_i form a prefix of the sorted order, found with binary search, so preMax[j] stores the maximum of val - end over the first j meetings. The answer is the maximum val over all meetings. Totals reach about 10^14, so use 64-bit integers.
Complexity: O(n log n) time and O(n) extra space.
class Solution:
def maxEarnings(self, meetings: List[List[int]]) -> int:
n = len(meetings)
meetings.sort(key=lambda x: x[1])
pre_max = [float("-inf")] * (n + 1)
ans = 0
for i, (start, end, revenue) in enumerate(meetings):
val = revenue
if start >= meetings[0][1]:
lo, hi = 0, i
while lo < hi:
mid = (lo + hi) // 2
if meetings[mid][1] <= start:
lo = mid + 1
else:
hi = mid
j = lo
val += pre_max[j] + start
ans = max(ans, val)
pre_max[i + 1] = max(pre_max[i], val - end)
return ans