EV Charging Cost Optimization
Problem statement
A fleet has charging requirements given by vehicles. Only the total required energy matters; energy from any charging slot may be assigned to any vehicle.
Each entry in costs represents one charging slot. A slot can provide at most maxCapacity units, and every unit taken from that slot costs the corresponding value in costs. Each slot may be used partially or skipped.
After using the slots, every unfulfilled unit costs penalty. Return the minimum total cost needed to cover or penalize the entire demand.
Implement minimumChargingCost with integer-array parameters vehicles and costs, integer parameters maxCapacity and penalty, and return the minimum total cost as a long.
Function
minimumChargingCost(vehicles: int[], costs: int[], maxCapacity: int, penalty: int) → longExamples
Example 1
vehicles = [5,7,3]costs = [10,4,8,20]maxCapacity = 6penalty = 9return = 99The total demand is 15. Use 6 units from the cost-4 slot and 6 units from the cost-8 slot, then pay the penalty for the remaining 3 units: 24 + 48 + 27 = 99.
Example 2
vehicles = [4,4]costs = [3,5]maxCapacity = 5penalty = 10return = 30Both slots are cheaper than the penalty. Buy 5 units at cost 3 and the remaining 3 units at cost 5.
Example 3
vehicles = [10]costs = [12,15]maxCapacity = 4penalty = 7return = 70Every charging slot is more expensive per unit than the penalty, so skip both slots and pay 10 * 7.
Constraints
1 <= vehicles.length, costs.length <= 2000000 <= vehicles[i] <= 10^91 <= costs[i], maxCapacity, penalty <= 10^9- The answer fits in a signed
64-bitinteger.