Booking rack: Even-key census
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. - Count the even numbers in
acceptedLengths. Store the count inevenCount. A number is even when it divides by 2 with no remainder. - Complete
bookingRackEvenKeyCensusso it returnsevenCount. Return it, do not print it.
Example
If drawers is [[3, 7], [8, 9], [-4, 3], [6, 10], [10, 17]], then acceptedLengths is [7, 4, 1, 7] and bookingRackEvenKeyCensus returns 1.
Rules
- If
acceptedLengthsis empty, return 0.
Test data
TEST DATA 1: drawers is [[-9, -6], [5, 10], [21, 27], [3, 4], [23, 29]].
TEST DATA 2: drawers is [[-2, 1], [5, 10], [6, 11], [7, 12]].
Examples
Input: drawers = [[-9,-6],[5,10],[21,27],[3,4],[23,29]] Output: 1
acceptedLengths is [3, 1, 5, 6]. The even numbers are [6]. There are 1, so return 1.
Input: drawers = [[-2,1],[5,10],[6,11],[7,12]] Output: 0
acceptedLengths is [3, 5]. The even numbers are []. There are 0, so return 0.
Input: drawers = [] Output: 0
acceptedLengths is []. The even numbers are []. There are 0, 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.