Food delivery companies employ tens of thousands of delivery drivers who each submit hundreds of deliveries per week. Delivery details are automatically sent to the system immediately after the delivery. Delivery drivers have different hourly payment rates, depending on their performance. Drivers can take on, and be paid for, multiple deliveries simultaneously.
If a driver is paid $10.00 per hour, and a delivery takes 1 hour and 30 minutes, the driver is paid $15.00 for that delivery. We are building a dashboard to show a single number, the total cost of all deliveries, on screens in the accounting department offices.
Implement the DeliveryCost class:
-
DeliveryCost()Initializes a new cost tracker with no drivers. -
void addDriver(int driverId, double usdHourlyRate)Registers a new driver using the unique integerdriverIdand assigns them an hourly pay rateusdHourlyRate. The given driver is guaranteed in the system before. -
void recordDelivery(int driverId, int startTime, int endTime)Records a delivery for the driver specified bydriverId, with start and end timesstartTimeandendTime, respectively.- Both times are Unix epoch timestamps in seconds and require at least one-second precision.
- Each delivery is guaranteed to be recorded immediately after it is completed.
-
double getTotalCost()Returns the sum of all drivers' delivery costs accumulated so far, as a double-precision floating point value.
Constraints:
≤ number of drivers ≤≤ number of deliveries ≤- Each
driverIdis an integer in - All
startTimeandendTimevalues are signed 32-bit integers representing seconds since the Unix epoch.
Example
Input: ["DeliveryCost", "addDriver", "getTotalCost", "recordDelivery", "getTotalCost", "addDriver", "recordDelivery", "getTotalCost", "recordDelivery", "getTotalCost", "addDriver", "recordDelivery", "getTotalCost"] [[], [1, 20.0], [], [1, 1000000000, 1000003600], [], [2, 30.0], [2, 1000003600, 1000005400], [], [1, 1000005400, 1000009000], [], [3, 25.0], [3, 1000009000, 1000012600], []]
Output: [null, null, 0.0, null, 20.0, null, null, 35.0, null, 55.0, null, null, 80.0]
Explanation:
DeliveryCost dc = new DeliveryCost();dc.addDriver(1, 20.0);dc.getTotalCost();// Returns 0.0, no deliveries have been recorded.dc.recordDelivery(1, 1000000000, 1000003600);// Records a 1-hour delivery for driver 1.dc.getTotalCost();// Returns 20.0, total cost for 1 hour at $20/hour.dc.addDriver(2, 30.0);dc.recordDelivery(2, 1000003600, 1000005400);// Records a 0.5-hour delivery for driver 2.dc.getTotalCost();// Returns 35.0, sum of $20 (driver 1) and $15 (driver 2).dc.recordDelivery(1, 1000005400, 1000009000);// Records another 1-hour delivery for driver 1.dc.getTotalCost();// Returns 55.0.dc.addDriver(3, 25.0);dc.recordDelivery(3, 1000009000, 1000012600);// Records a 1-hour delivery for driver 3.dc.getTotalCost();// Returns 80.0
