NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

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 integer driverId and assigns them an hourly pay rate usdHourlyRate. The given driver is guaranteed in the system before.

  • void recordDelivery(int driverId, int startTime, int endTime) Records a delivery for the driver specified by driverId, with start and end times startTime and endTime, 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:

  • 11 ≤ number of drivers ≤ 10001000
  • 11 ≤ number of deliveries ≤ 10001000
  • Each driverId is an integer in [1,105][1, 10^5]
  • All startTime and endTime values 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

Accepted
4.6k/6.3k
Acceptance Rate72.7%
Test Cases
Input
Output
Expected