Paid reservations: Positive-ink invoice
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. - Add only the numbers in
bestRewardsthat are greater than 0. Store the sum inpositiveTotal. - Complete
paidReservationsPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If drawers is [[7, 8, 7], [8, 9, 8], [9, 10, 9], [8, 14, 8], [-1, 5, 9]], then bestRewards is [9, 16, 24, 33, 33] and paidReservationsPositiveInkInvoice returns 115.
Rules
- Skip zeros.
- If no number is greater than 0, return 0.
Test data
TEST DATA 1: drawers is [[-8, -6, 8], [20, 26, 20], [2, 3, 2], [22, 28, 22], [10, 17, 10]].
TEST DATA 2: drawers is [[2, 5, 2], [3, 6, 3], [4, 7, 4], [-2, 4, 8]].
Examples
Input: drawers = [[-8,-6,8],[20,26,20],[2,3,2],[22,28,22],[10,17,10]] Output: 120
bestRewards is [8, 10, 20, 40, 42]. Keep [8, 10, 20, 40, 42] and add those numbers. Return 120.
Input: drawers = [[2,5,2],[3,6,3],[4,7,4],[-2,4,8]] Output: 36
bestRewards is [8, 8, 8, 12]. Keep [8, 8, 8, 12] and add those numbers. Return 36.
Input: drawers = [] Output: 0
bestRewards is []. Keep [] and add those numbers. 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.