Paid reservations: Boss 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. - Multiply each number in
bestRewardsby its position, counting from 1: the first number by 1, the second by 2, and so on. - Add those products. Store the sum in
weightedTotal. - Complete
paidReservationsBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If drawers is [[6, 13, 6], [4, 8, 4], [-5, -4, 9], [10, 11, 10], [10, 17, 10]], then bestRewards is [9, 13, 23, 23, 23] and paidReservationsBossReceipt returns 311.
Rules
- If
bestRewardsis empty, return 0.
Test data
TEST DATA 1: drawers is [[19, 25, 19], [20, 26, 20], [9, 10, 9], [2, 4, 4], [4, 5, 4]].
TEST DATA 2: drawers is [[-5, 1, 5], [8, 9, 8], [9, 10, 9], [10, 11, 10]].
Examples
Input: drawers = [[19,25,19],[20,26,20],[9,10,9],[2,4,4],[4,5,4]] Output: 400
bestRewards is [4, 8, 17, 36, 37]. Multiply by positions that start at 1: 1 * (4) + 2 * (8) + 3 * (17) + 4 * (36) + 5 * (37) = 400. Return 400.
Input: drawers = [[-5,1,5],[8,9,8],[9,10,9],[10,11,10]] Output: 225
bestRewards is [5, 13, 22, 32]. Multiply by positions that start at 1: 1 * (5) + 2 * (13) + 3 * (22) + 4 * (32) = 225. Return 225.
Input: drawers = [] Output: 0
bestRewards is []. There are no numbers to multiply or add, 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.