885. Fuel-Limited Routes

A delivery drone serves depots placed on a line: depot i is at position locations[i], and all positions are distinct. The drone begins at depot start with fuel units of fuel. Flying from depot i to a different depot j costs exactly |locations[i] - locations[j]| fuel, and the drone can never let its fuel go negative. It may pass through any depot, including start and finish, as many times as it likes, and it need not use all its fuel.

Count the distinct routes (sequences of depots beginning at start) that end at depot finish. The route that is just start counts when start == finish. Return the count modulo 1000000007.

Example 1

Input:
locations = [2,5,6]start = 0finish = 2fuel = 5
Output:
2
Explanation:

With fuel 5 the valid routes ending at the depot at 6 are the direct flight (cost 4) and the route via the depot at 5 (cost 3 + 1), so there are 2.

Example 2

Input:
locations = [10,20]start = 0finish = 1fuel = 5
Output:
0
Explanation:

The two stops are 10 apart but only 5 fuel is available, so the destination is unreachable: 0 routes.

Example 3

Input:
locations = [3,4]start = 0finish = 0fuel = 2
Output:
2
Explanation:

Staying put counts as one route (start equals destination); a round trip 3->4->3 costs 2 and is a second route: total 2.

Constraints

  • 2 ≤ locations.length ≤ 100
  • 1 ≤ locations[i] ≤ 109, all distinct
  • 0 ≤ start, finish < locations.length
  • 1 ≤ fuel ≤ 200

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(fuel * n^2)
Space
O(fuel * n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…