Ctrl K

Trips on One Tank

Hard
Rest stops line a highway; stops[i] is the coordinate of stop i, and all coordinates are distinct. You are at stop origin with fuel units of fuel and need to end up at stop target. There is no refuelling on the way. In one move you can drive from your current stop i to any other stop j, which burns |stops[i] - stops[j]| units of fuel. Fuel can never go below zero. Any stop, including origin and target, may be visited any number of times. A trip is a sequence of stops that starts at origin and ends at target. Count the distinct trips. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
stops=[1, 4, 6], origin=0, target=2, fuel=5 → 2

Two trips work: 0 → 2 (5 units of fuel) and 0 → 1 → 2 (3 + 2 = 5). Any other trip needs more fuel.

Example 2
stops=[10, 12], origin=0, target=0, fuel=4 → 2

You can stay put (a trip that is just stop 0) or go 0 → 1 → 0 for 4 units. That makes 2.

Example 3
stops=[3, 20, 7], origin=1, target=0, fuel=16 → 0

Even the direct way from 20 to 3 costs 17, which is more than the fuel available.

Constraints
  • 2 ≤ len(stops) ≤ 100
  • 1 ≤ stops[i] ≤ 10⁹, all values distinct
  • 0 ≤ origin, target < len(stops)
  • 1 ≤ fuel ≤ 200
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Way to Space the Words HardLongest Uphill Walk HardClearing Colored Cubes HardSix-Cell Tile Slider HardCutting a Berry Cake Hard
loading Python…
Input
stops=[1, 4, 6], origin=0, target=2, fuel=5
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.