Ring Line Tram Battery
A city is testing a battery tram on its ring line. Stops are numbered 0 to n - 1, and after the last stop the tram returns to stop 0. Waiting at stop i adds charge[i] units; driving from stop i to the next uses cost[i].
The tram starts at one stop with a flat battery and must never run out between stops. Where should the engineers place it so it can drive one full loop?
Your task
Write startingStop(charge, cost). The tram starts at stop s with 0 units, adds charge[s], spends cost[s] to reach the next stop, adds that stop's charge, and so on, until it is back at s. The battery must never drop below 0 on arrival at a stop. Return the smallest stop index from which the full loop works, or -1 if none does.
The battery has no upper limit. Arriving with exactly 0 units is fine.
startingStop(charge: int[], cost: int[]) → int
Examples
charge = [3, 1, 2, 4] cost = [2, 3, 3, 1]
3
Starting at stop 3, the tram charges 4 units and drives 1, so it arrives at stop 0 with 3. It charges 3 (total 6), drives 2 and arrives at stop 1 with 4. It charges 1 (total 5), drives 3 and arrives at stop 2 with 2. It charges 2 (total 4), drives 3 and is back at stop 3 with 1. Starting at stop 0 fails: the tram leaves with 3 - 2 = 1, reaches stop 1, and then 1 + 1 - 3 = -1 before it can reach stop 2. Stops 1 and 2 already fail on their first drive.
charge = [2, 2, 2] cost = [3, 2, 2]
-1
The pads give 6 units in total, but the loop costs 7. No start can make up for that.
Constraints
- 1 ≤ charge.length = cost.length ≤ 100,000
- 0 ≤ charge[i], cost[i] ≤ 10,000
Start with brute force
Try every stop as the start and simulate a full loop, stopping early if the battery goes negative.
Each simulation takes up to n steps, so the total is O(n²), which is up to 10 billion steps for 100,000 stops. Much of that work is wasted. If a run from stop 2 fails at stop 7, the approach then tries stops 3 to 6, and they are doomed to fail for the same reason.
Hints
0 of 4 shown- Hint 1
Replace each stop by its net gain,
charge[i] - cost[i]. In the first example that is[1, -2, -1, 3]. When is the loop impossible from every start? - Hint 2
If the net gains sum to less than 0, the answer is -1. If they sum to 0 or more, some start always works, and the remaining task is to find which one.
- Hint 3
Drive from stop 0, adding net gains. Suppose the battery first goes negative leaving stop
i. Could any stop between your start andihave worked instead? Each would reachiwith no more charge than you had. - Hint 4
Sweep once with a running
tankand candidatestart = 0. Addcharge[i] - cost[i]; if the tank drops below 0, setstart = i + 1andtank = 0. Separately sum the total net gain. Returnstartif the total is at least 0, otherwise -1.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Two facts make one pass enough.
- A failed run rules out its whole stretch. If a run from
startdies leaving stopi, every stopkin between was reached with 0 or more. Starting fresh atkwith 0 gives no more charge ati, so it dies too. Skip toi + 1. - If the total net gain is at least 0, the last candidate works. The skipped stretches all had negative sums, so the stretch from the final candidate to the end carries enough surplus to cover them.
Trace the first example. Net gains: [1, -2, -1, 3].
- At i = 0 the tank is 1, which is not negative, so the candidate stays at 0.
- At i = 1 the tank is 1 - 2 = -1, so the run fails. Stops 0 and 1 are ruled out, and the code sets start = 2 and tank = 0.
- At i = 2 the tank is -1, so the run fails again. The code sets start = 3 and tank = 0.
- At i = 3 the tank is 3, which is not negative, so the candidate stays at 3.
Total = 1 - 2 - 1 + 3 = 1 ≥ 0, so the answer is 3. Every earlier stop was ruled out, so it is also the smallest that works.
function startingStop(charge, cost) {
let total = 0; // net gain of the whole loop
let tank = 0; // battery since the current candidate start
let start = 0;
for (let i = 0; i < charge.length; i++) {
const gain = charge[i] - cost[i];
total += gain;
tank += gain;
if (tank < 0) {
// No stop from start to i can work. Try the next one.
start = i + 1;
tank = 0;
}
}
return total >= 0 ? start : -1;
}def starting_stop(charge, cost):
total = 0 # net gain of the whole loop
tank = 0 # battery since the current candidate start
start = 0
for i in range(len(charge)):
gain = charge[i] - cost[i]
total += gain
tank += gain
if tank < 0:
# No stop from start to i can work. Try the next one.
start = i + 1
tank = 0
return start if total >= 0 else -1Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.