Booking rack: Total receipt
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 up every number in
acceptedLengths. Store the sum intotal. - Complete
bookingRackTotalReceiptso it returnstotal. Return it, do not print it.
Example
If drawers is [[2, 5], [8, 9], [4, 7], [1, 4], [8, 13]], then acceptedLengths is [3, 3, 1] and bookingRackTotalReceipt returns 7.
Rules
- If
acceptedLengthsis empty,totalis 0.
Test data
TEST DATA 1: drawers is [[15, 17], [1, 2], [-10, -4], [13, 17], [14, 18]].
TEST DATA 2: drawers is [[8, 10], [9, 11], [10, 12], [-6, -3]].
Examples
Input: drawers = [[15,17],[1,2],[-10,-4],[13,17],[14,18]] Output: 11
acceptedLengths is [6, 1, 4]. Add its numbers to get 11. Return 11.
Input: drawers = [[8,10],[9,11],[10,12],[-6,-3]] Output: 7
acceptedLengths is [3, 2, 2]. Add its numbers to get 7. Return 7.
Input: drawers = [] Output: 0
acceptedLengths is []. Add its numbers to get 0. 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.