Dasher Active Time
Learn this problemProblem statement
A Dasher processes an ordered sequence of delivery events during one day. The arrays timestamps and actions have equal length. Event i occurs at minute timestamps[i] from the start of the day, and actions[i] is either PICKUP or DROPOFF.
A pickup adds one active delivery. A dropoff completes one active delivery. The sequence is valid: no prefix has more dropoffs than pickups, and every pickup is matched by the final event.
The Dasher is active whenever at least one delivery is active. Overlapping deliveries do not multiply active time. Return the total number of minutes during which the Dasher is active. If there are no events, return 0.
Timestamps are nondecreasing and each timestamp is between minute 0 and minute 1440, inclusive. When several events have the same timestamp, process them in their given order; because no time passes between them, their order does not itself add active minutes.
Function
totalActiveTime(timestamps: int[], actions: String[]) → intExamples
Example 1
timestamps = [510,550,620,735,765,865]actions = ["PICKUP","DROPOFF","PICKUP","PICKUP","DROPOFF","DROPOFF"]return = 285The active ranges are [510,550) and [620,865). Their lengths are 40 and 245, for 285 active minutes. The overlapping deliveries from minute 735 through 765 still contribute time only once.
Example 2
timestamps = [0,5,10,20]actions = ["PICKUP","PICKUP","DROPOFF","DROPOFF"]return = 20The active-delivery count stays positive from minute 0 until minute 20, so the total is 20.
Example 3
timestamps = [10,10,20,20]actions = ["PICKUP","DROPOFF","PICKUP","DROPOFF"]return = 0Each delivery is picked up and dropped off at the same minute, so neither lifecycle contains a positive-length active interval.
Constraints
0 <= timestamps.length <= 100000.actions.length = timestamps.length.0 <= timestamps[i] <= 1440.timestampsis in nondecreasing order.- Every action is
PICKUPorDROPOFF. - In every prefix, the number of dropoffs is at most the number of pickups.
- The total number of pickups equals the total number of dropoffs.