home. It hops by these rules:
- a forward hop moves exactly forward to the right;
- a back hop moves exactly back to the left;
- it never makes two back hops in a row;
- it may never land on a negative point or on any point listed in traps.
Overshooting is allowed: the grasshopper may go past home and come back later.
Return the fewest hops that land the grasshopper exactly on home, or -1 if that is impossible.traps=[2, 9], forward=4, back=3, home=5 → 3
For example 0 → 4 → 1 → 5: forward, back, forward. Position 5 cannot be reached in fewer than three hops.
traps=[4], forward=4, back=1, home=3 → -1
The forward hop lands on the trap at 4 and a backward hop from 0 would go negative, so the grasshopper cannot move.
traps=[1], forward=2, back=1, home=0 → 0
The grasshopper is already home.
traps are distincthome is not in trapstraps=[2, 9], forward=4, back=3, home=5
3
Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.