FastPrepFastPrep
Problem Brief

Compute Walking Distance

FULLTIMEOA

Starting from position 0, you need to carry cargo to a target position. Along the way there are charging stations at various positions. The process works as follows:

  • Walk with the cargo to the nearest charging station ahead of your current position.
  • Use a drone at that station to carry the cargo forward by 10 units (i.e., from station position p to p + 10).
  • Repeat until the cargo reaches or passes the target.
  • If no station is ahead, walk the remaining distance on foot.

Compute the total distance you actually carry the cargo on foot (excluding drone-traveled parts).

Approach: Sort the stations. Each round, find the next usable station ahead of your current position, add the walking distance to reach it, then update your position to station + 10. Repeat until reaching or passing the target.

1Example 1

Input
target = 15, stations = [5]
Output
5
Explanation
Walk from 0 to station 5 (5 units on foot). Drone carries cargo from 5 to 15. Position 15 >= target 15, done. Total walking distance = 5.

2Example 2

Input
target = 25, stations = [5,20]
Output
10
Explanation
Walk from 0 to station 5 (5 units). Drone carries 5 → 15. Walk from 15 to station 20 (5 units). Drone carries 20 → 30. Position 30 >= target 25, done. Total walking = 5 + 5 = 10.
public int computeWalkingDistance(int target, int[] stations) {
  // write your code here
}
Input

target

15

stations

[5]

Output

5

Sign in to submit your solution.