Booking rack: Proof strip
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. - Complete
bookingRackProofStripso it returnsacceptedLengths. Return it, do not print it.
Example
If drawers is [[3, 7], [9, 11], [14, 20], [6, 10], [0, 5]], then acceptedLengths is [5, 4, 6] and bookingRackProofStrip returns [5, 4, 6].
Rules
- If
acceptedLengthsis empty, return [].
Test data
TEST DATA 1: drawers is [[7, 8], [8, 9], [2, 3], [17, 18], [-8, -2]].
TEST DATA 2: drawers is [[3, 7], [0, 2], [5, 9], [6, 10]].
Examples
Input: drawers = [[7,8],[8,9],[2,3],[17,18],[-8,-2]] Output: [6,1,1,1,1]
acceptedLengths is [6, 1, 1, 1, 1]. bookingRackProofStrip returns that whole list, so return [6, 1, 1, 1, 1].
Input: drawers = [[3,7],[0,2],[5,9],[6,10]] Output: [2,4]
acceptedLengths is [2, 4]. bookingRackProofStrip returns that whole list, so return [2, 4].
Input: drawers = [] Output: []
acceptedLengths is []. bookingRackProofStrip returns that whole list, so 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 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.