Booking rack: Positive-ink invoice
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. - Add only the numbers in
acceptedLengthsthat are greater than 0. Store the sum inpositiveTotal. - Complete
bookingRackPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If drawers is [[-2, 1], [4, 8], [7, 13], [6, 10], [6, 9]], then acceptedLengths is [3, 4] and bookingRackPositiveInkInvoice returns 7.
Rules
- If
acceptedLengthsis empty, return 0.
Test data
TEST DATA 1: drawers is [[11, 16], [-6, -5], [19, 23], [3, 4], [15, 20]].
TEST DATA 2: drawers is [[9, 12], [10, 13], [11, 14], [2, 4]].
Examples
Input: drawers = [[11,16],[-6,-5],[19,23],[3,4],[15,20]] Output: 11
acceptedLengths is [1, 1, 5, 4]. Keep [1, 1, 5, 4] and add those numbers. Return 11.
Input: drawers = [[9,12],[10,13],[11,14],[2,4]] Output: 5
acceptedLengths is [2, 3]. Keep [2, 3] and add those numbers. Return 5.
Input: drawers = [] Output: 0
acceptedLengths is []. Keep [] and add those numbers. Return 0.
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.