Paid reservations: Threshold file
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. - Count the numbers in
bestRewardsthat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
paidReservationsThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If drawers is [[3, 7, 3], [-7, -5, 9], [7, 13, 7], [11, 13, 11], [12, 14, 12]] and k is 8, then bestRewards is [9, 12, 19, 23, 24] and paidReservationsThresholdFile returns 5.
Rules
- If
bestRewardsis empty, the count is 0. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: drawers is [[16, 19, 16], [10, 13, 10], [-10, -4, 14], [19, 22, 19], [4, 5, 4]] and k is 5.
TEST DATA 2: drawers is [[-9, -6, 9], [6, 12, 6], [7, 13, 7], [8, 14, 8]] and k is 7.
Examples
Input: drawers = [[16,19,16],[10,13,10],[-10,-4,14],[19,22,19],[4,5,4]] k = 5 Output: 5
bestRewards is [14, 18, 28, 44, 63]. Here k is 5. The numbers at least 5 are [14, 18, 28, 44, 63]. There are 5, so return 5.
Input: drawers = [[-9,-6,9],[6,12,6],[7,13,7],[8,14,8]] k = 7 Output: 4
bestRewards is [9, 15, 16, 17]. Here k is 7. The numbers at least 7 are [9, 15, 16, 17]. There are 4, so return 4.
Input: drawers = [] k = 6 Output: 0
bestRewards is []. Here k is 6. The numbers at least 6 are []. There are 0, so return 0.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. 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.