A trip planner for an electric scooter must reach target kilometers away. The scooter starts with startCharge units of charge and uses 1 unit per kilometer. Charging stations are listed in stations as [position, charge], sorted by position: stopping there adds charge units (the battery has no limit).
Write fewestStops(target, startCharge, stations) that returns the fewest charging stops needed to reach target, or -1 if it can't be reached. Arriving somewhere with exactly 0 charge left still counts as arriving.
Input: target = 100, startCharge = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2
Stop at 10 (charge 60, reach 70), then at 60 (charge 40, reach 110).
Input: target = 100, startCharge = 1, stations = [[10,100]] Output: -1
The scooter can't even reach the first station.
1 <= target, startCharge <= 10^90 <= stations.length <= 10^50 < position < target, positions increase1 <= charge <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #594 Fewest Recharge Stops. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.