Booking rack: First qualifying slip
Inputs
drawers is a list of bookings. Each row is [start, end].
Your tasks
- Create a list
acceptedLengths. Each booking is [start, end]. It uses time from start up to, but not including, end. Sort bookings by end time, from earliest to latest. For matching end times, put the earlier start first. Accept the first booking. Then accept a booking only if it starts at or after the end of the last one accepted. Record the length, end - start, of each accepted booking. Keep acceptance order. Negative times are allowed. With no bookings,acceptedLengthsis empty. - Read
acceptedLengthsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
bookingRackFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If drawers is [[-6, 1], [5, 10], [6, 11], [10, 11], [12, 14]] and k is 4, then acceptedLengths is [7, 5, 1, 2] and bookingRackFirstQualifyingSlip 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 [[-5, 1], [14, 21], [2, 3], [16, 23], [21, 25]] and k is 8.
TEST DATA 2: drawers is [[2, 5], [-7, -5], [4, 7], [5, 8]] and k is 2.
Examples
Input: drawers = [[-5,1],[14,21],[2,3],[16,23],[21,25]] k = 8 Output: -1
acceptedLengths is [6, 1, 7, 4]. Here k is 8. No number is at least 8, so return -1.
Input: drawers = [[2,5],[-7,-5],[4,7],[5,8]] k = 2 Output: 2
acceptedLengths is [2, 3, 3]. Here k is 2. The first number at least 2 is 2. Return that value.
Input: drawers = [] k = 5 Output: -1
acceptedLengths is []. Here k is 5. No number is at least 5, 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 acceptedLengths 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.