Paid reservations: Peak ticket
Stuck or curious how this works? Visualize Binary Search ↗
Inputs
drawers is a list of bookings. Each row is [start, end, reward].
Your tasks
- Create a list
bestRewards. Each row ofdrawersis a booking: [start, end, reward]. It uses time from start up to, but not including, end. A booking may start exactly when another one ends. Sort the bookings by end time, then start time, then reward, all from smallest to largest. Look at the first booking, then the first two bookings, and so on. For each of these groups, choose bookings that do not overlap and give the largest total reward. Record that reward. You may choose no bookings for a reward of 0. With no bookings,bestRewardsis empty. - Find the largest number in
bestRewards. Store it inlargest. - Complete
paidReservationsPeakTicketso it returnslargest. Return it, do not print it.
Example
If drawers is [[2, 5, 2], [1, 2, 1], [-5, -4, 9], [5, 8, 5], [8, 13, 8]], then bestRewards is [9, 10, 12, 17, 25] and paidReservationsPeakTicket returns 25.
Rules
- If
bestRewardsis empty, return 0.
Test data
TEST DATA 1: drawers is [[-10, -6, 10], [14, 21, 14], [5, 9, 5], [3, 4, 3], [7, 11, 7]].
TEST DATA 2: drawers is [[-3, 1, 3], [7, 14, 7], [8, 15, 8], [9, 16, 9]].
Examples
Input: drawers = [[-10,-6,10],[14,21,14],[5,9,5],[3,4,3],[7,11,7]] Output: 34
bestRewards is [10, 13, 18, 20, 34]. The largest number is 34. Return 34.
Input: drawers = [[-3,1,3],[7,14,7],[8,15,8],[9,16,9]] Output: 12
bestRewards is [3, 10, 11, 12]. The largest number is 12. Return 12.
Input: drawers = [] Output: 0
bestRewards is []. The list is empty, so return 0.
Input limits
- 0 <= input size <= 64.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n log n); extra memory, including the helper list, O(n). n is the input length. These labels describe how work and memory grow with the input.
Practice focus
Build bestRewards first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.