Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
def num_charging_stops(legs: List[int], capacity: int) -> int:
Calculates the number of necessary charging stops to reach the destination.

Each "leg" of your journey represents the amount of energy required to drive
that leg of the trip. For example, if a leg has a value of 5 and your
battery's energy level before driving the leg is 12, then driving that leg
will drop the battery's energy level to 7 (12-5). After driving each leg, you
have the choice of whether to charge or not. If you decide to charge, your
battery's energy level get's returned to "capacity". If you decide not to
charge, your battery's energy level is unchanged. The goal is to calculate
the minimum number of charging stops needed to reach the destination without
ever letting your battery's level fall below 0. If it isn't possible to reach
the destination with the given inputs, -1 should be returned.

Assume that your battery is charged to full capacity to start.

What I have tried:

not been able to find a solution
Posted
Updated 22-Nov-23 20:52pm
v4
Comments
PIEBALDconsult 21-Nov-23 21:21pm    
We won't do your homework for you.
[no name] 22-Nov-23 1:00am    
Or pander to your interest in Tesla.

1 solution

It is not such a daunting task.
Keep track of current charge and check if next leg is reachable without recharging. If it is not, then charge (or give up if you are at the starting point).
This is rather naive. But it is a start, anyway. Probably a full featured solution will need a bit of backtracking.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900