Paid reservations: Positive proof strip
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. - Create a list
positiveValuesthat keeps only the numbers inbestRewardsthat are greater than 0, in the same order. - Complete
paidReservationsPositiveProofStripso it returnspositiveValues. Return it, do not print it.
Example
If drawers is [[6, 13, 6], [8, 9, 8], [-2, 3, 6], [13, 17, 13], [11, 12, 11]], then bestRewards is [6, 14, 25, 25, 38] and paidReservationsPositiveProofStrip returns [6, 14, 25, 25, 38].
Rules
- Keep repeated numbers.
- If no number is greater than 0, return [].
Test data
TEST DATA 1: drawers is [[20, 27, 20], [1, 2, 1], [22, 29, 22], [13, 17, 13], [-3, -2, 11]].
TEST DATA 2: drawers is [[3, 7, 3], [4, 8, 4], [5, 9, 5], [-6, -3, 12]].
Examples
Input: drawers = [[20,27,20],[1,2,1],[22,29,22],[13,17,13],[-3,-2,11]] Output: [11,12,25,45,47]
bestRewards is [11, 12, 25, 45, 47]. Keep only numbers greater than 0, without changing their order. Return [11, 12, 25, 45, 47].
Input: drawers = [[3,7,3],[4,8,4],[5,9,5],[-6,-3,12]] Output: [12,15,16,17]
bestRewards is [12, 15, 16, 17]. Keep only numbers greater than 0, without changing their order. Return [12, 15, 16, 17].
Input: drawers = [] Output: []
bestRewards is []. Keep only numbers greater than 0, without changing their order. Return [].
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.