Paid reservations: Total receipt
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 up every number in
bestRewards. Store the sum intotal. - Complete
paidReservationsTotalReceiptso it returnstotal. Return it, do not print it.
Example
If drawers is [[6, 13, 6], [10, 13, 10], [4, 7, 4], [-3, 4, 9], [10, 17, 10]], then bestRewards is [9, 13, 15, 23, 23] and paidReservationsTotalReceipt returns 83.
Rules
- If
bestRewardsis empty,totalis 0.
Test data
TEST DATA 1: drawers is [[2, 5, 2], [3, 6, 3], [17, 19, 17], [3, 4, 3], [-8, -2, 16]].
TEST DATA 2: drawers is [[6, 13, 6], [-1, 2, 3], [8, 15, 8], [9, 16, 9]].
Examples
Input: drawers = [[2,5,2],[3,6,3],[17,19,17],[3,4,3],[-8,-2,16]] Output: 109
bestRewards is [16, 19, 19, 19, 36]. Add its numbers to get 109. Return 109.
Input: drawers = [[6,13,6],[-1,2,3],[8,15,8],[9,16,9]] Output: 35
bestRewards is [3, 9, 11, 12]. Add its numbers to get 35. Return 35.
Input: drawers = [] Output: 0
bestRewards is []. Add its numbers to get 0. 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.