Trains the technique from
LeetCode 871Minimum Number of Refueling StopsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
An electric haul truck runs one straight corridor, entering at milepost 0 and finishing at milepost distance. Its pack begins the shift holding start_charge units, and one unit carries the truck exactly one milepost forward. The pack has no ceiling, so it can hold any amount of charge at once.
Charging pads sit along the corridor. Entry pads[i] = [milepost_i, units_i] says a pad waits at milepost_i holding units_i units of charge. Pulling into a pad tips that pad's whole reserve into the pack; partial draws are not possible, and the driver is free to roll past a pad without stopping. Pads are listed by increasing milepost. Coasting in with the pack reading exactly zero still counts as arriving, at a pad or at the corridor's end.
Work out the smallest number of pads the truck has to pull into so that it finishes the corridor. When no choice of pads carries it that far, the answer is -1.
Example 1
Roll past the pad at milepost 15 and take the 50 units waiting at milepost 25, which lifts the pack to 75 units of range. From there the pad at milepost 70 adds 30 more, reaching 105 units, which clears milepost 90 after only two halts.
Example 2
The opening 10 units already reach milepost 10, and the 50 units there lift the pack to 60 units of range, which clears the corridor.
Example 3
The only pad lifts the pack to 8 units of range, which strands the truck at milepost 8 with nothing else in reach.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fewest_charging_halts(distance: int, start_charge: int, pads: list[list[int]]) -> int:public int fewestChargingHalts(int distance, int startCharge, int[][] pads)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.