Booking rack: Boss 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. - Multiply each number in
acceptedLengthsby its position, counting from 1: the first number by 1, the second by 2, and so on. - Add those products. Store the sum in
weightedTotal. - Complete
bookingRackBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If drawers is [[5, 11], [9, 11], [9, 10], [8, 14], [-3, -2]], then acceptedLengths is [1, 1] and bookingRackBossReceipt returns 3.
Rules
- If
acceptedLengthsis empty, return 0.
Test data
TEST DATA 1: drawers is [[15, 17], [1, 2], [19, 23], [-4, -3], [21, 25]].
TEST DATA 2: drawers is [[9, 12], [10, 13], [-5, -4], [12, 15]].
Examples
Input: drawers = [[15,17],[1,2],[19,23],[-4,-3],[21,25]] Output: 25
acceptedLengths is [1, 1, 2, 4]. Multiply by positions that start at 1: 1 * (1) + 2 * (1) + 3 * (2) + 4 * (4) = 25. Return 25.
Input: drawers = [[9,12],[10,13],[-5,-4],[12,15]] Output: 16
acceptedLengths is [1, 3, 3]. Multiply by positions that start at 1: 1 * (1) + 2 * (3) + 3 * (3) = 16. Return 16.
Input: drawers = [] Output: 0
acceptedLengths is []. There are no numbers to multiply or add, so 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.