Maximum Earnings from Back-to-Back Booking Slots
Dynamic ProgrammingAuto-graded · 18 test casesLast updated
Pick non-overlapping time slots, where one may begin exactly as another finishes, to maximize total payout; tests weighted interval scheduling with sorting, DP and binary search.
You manage a single resource, such as a meeting room, that can serve only one booking at a time. You are given a list of booking requests. Each request has a start time, a finish time and a payout. You may accept any subset of requests, as long as no two accepted bookings overlap in time. Return the largest total payout you can collect.
Input
bookings: a list where each element is[start, finish, payout], with three integers.
The booking i occupies the resource from start until finish.
Overlap rule
Two accepted bookings conflict only if their time ranges share some positive length of time. If one booking finishes at time t and another starts at exactly time t, they do not conflict, and both may be accepted.
You may accept any number of bookings, including just one. Each booking can be accepted at most once. If the list contains identical entries, they count as separate bookings, but any two of them overlap, so at most one of them can be accepted.
Output
Return a single integer: the maximum total payout over all valid selections.
Constraints
1 <= len(bookings) <= 5 * 10^41 <= start < finish <= 10^9for every booking1 <= payout <= 10^4for every booking- The answer is therefore at most
5 * 10^8. - The input list should not need to be modified.
Examples
Example 1
Input: max_booking_payout([[1, 4, 20], [2, 6, 30], [4, 7, 25], [7, 9, 15]])
Output: 60
Example 2
Input: max_booking_payout([[1, 2, 5], [1, 3, 6], [2, 3, 4]])
Output: 9
Example 3
Input: max_booking_payout([[1, 10, 100], [2, 3, 30], [3, 4, 30], [4, 5, 30]])
Output: 100
Hints
Hint 1
Simple greedy rules such as 'earliest finish first' or 'largest payout first' fail when payouts differ. Think about a decision for each booking: take it or skip it.
Hint 2
Sort the bookings by finish time. Let best[i] be the best payout using only the first i bookings in that order. When you take booking k, every other chosen booking must finish at or before its start.
Hint 3
In finish-time order, the bookings compatible with booking k that come before it form a prefix. Find the prefix length with binary search over the finish times, using a search that counts finish times less than or equal to the start. Then best[k+1] = max(best[k], best[prefix] + payout).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is weighted interval scheduling. Greedy choices fail when payouts differ: in the third sample, the three short bookings total 90, but the single long one pays 100. So we use dynamic programming.
- Sort the bookings by finish time.
- Let
best[i]be the maximum payout using only the firstibookings in this order. Setbest[0] = 0. - For booking
kwith(start, finish, payout)there are two options:- Skip it: the value is
best[k]. - Take it: every other chosen booking must finish at or before
start. Letidxbe the number of bookings whose finish time is at moststart. The value isbest[idx] + payout.
- Skip it: the value is
- So
best[k+1] = max(best[k], best[idx] + payout). The answer isbest[n].
To find idx, binary search the sorted finish times with bisect_right, which counts values less than or equal to start. This implements the rule that a booking finishing at t and one starting at t are compatible. Using bisect_left would wrongly forbid touching bookings.
Why it is correct
- Prefix property: in finish order, if booking
jfinishes at or beforestart, every earlier booking does too. The bookings compatible withkamong those before it therefore form a prefix[0, idx), andbest[idx]is the optimum over exactly that set. - Optimal substructure: an optimal selection from the first
k+1bookings either excludes bookingk, givingbest[k], or includes it, in which case the rest lies in the compatible prefix, givingbest[idx] + payout. Taking the maximum covers both cases. - Search range: any booking with finish at most
startfinishes strictly before bookingk, so it sorts earlier. Restricting the search to indices belowkloses nothing. - Equal finish times: bookings sharing a finish time always overlap each other (each starts before that finish), so their relative order does not matter.
Complexity
- Time: O(n log n) for the sort plus one binary search per booking.
- Space: O(n) for the sorted copy, the finish-time list and the DP array.
The DP is indexed by booking, not by time value, so times up to 10^9 add no cost. The input list is not modified because sorted returns a new list.
Reference solution
from bisect import bisect_right
from typing import List
def max_booking_payout(bookings: List[List[int]]) -> int:
# Sort by finish time so compatible predecessors form a prefix.
items = sorted(bookings, key=lambda b: b[1])
finishes = [b[1] for b in items]
# best[i] = maximum payout using only the first i bookings in sorted order.
best = [0] * (len(items) + 1)
for k, (start, finish, payout) in enumerate(items):
# Count earlier bookings finishing at or before this start.
idx = bisect_right(finishes, start, 0, k)
take = best[idx] + payout
best[k + 1] = take if take > best[k] else best[k]
return best[-1]