Trains the technique from
LeetCode 818Race CarThis 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.
A trolley stands at position 0 on a rail, facing forwards with a speed of 1.
Two controls are available:
1 when it was negative and to -1 when it was positive. The trolley does not move.Return the fewest presses needed to bring the trolley to position target.
Example 1
One push carries the trolley one place and it has arrived.
Example 2
Three pushes carry the trolley 1, then 2, then 4 places, which is exactly seven.
Example 3
Push twice to reach 3, then turn twice to bring the speed back to one without moving, and push once more to land on 4. A third push would have overshot to 7.
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 racecar(target: int) -> int:public int racecar(int target)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.