Paid reservations: First qualifying slip
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. - Read
bestRewardsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
paidReservationsFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If drawers is [[6, 13, 6], [6, 12, 6], [7, 13, 7], [8, 14, 8], [1, 5, 7]] and k is 4, then bestRewards is [7, 13, 13, 14, 15] and paidReservationsFirstQualifyingSlip returns 7.
Rules
- If no number is at least
k, return -1. 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 [[2, 5, 2], [3, 6, 3], [9, 10, 9], [-4, -3, 10], [4, 5, 4]] and k is 1.
TEST DATA 2: drawers is [[6, 13, 6], [7, 14, 7], [-5, -4, 9], [9, 16, 9]] and k is 2.
Examples
Input: drawers = [[2,5,2],[3,6,3],[9,10,9],[-4,-3,10],[4,5,4]] k = 1 Output: 10
bestRewards is [10, 12, 14, 14, 23]. Here k is 1. The first number at least 1 is 10. Return that value.
Input: drawers = [[6,13,6],[7,14,7],[-5,-4,9],[9,16,9]] k = 2 Output: 9
bestRewards is [9, 15, 16, 18]. Here k is 2. The first number at least 2 is 9. Return that value.
Input: drawers = [] k = 4 Output: -1
bestRewards is []. Here k is 4. No number is at least 4, so return -1.
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.