Problem · Array
Compute Walking Distance
Learn this problemProblem statement
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
ptop + 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.
Function
computeWalkingDistance(target: int, stations: int[]) → intExamples
Example 1
target = 15stations = [5]return = 5Walk 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.
Example 2
target = 25stations = [5,20]return = 10Walk 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.
More Tiktok problems
- Can Reach the Exit with TeleportsOA · Seen Jul 2026
- Check Monotonic TriplesOA · Seen Jul 2026
- Shift Every K-th ConsonantOA · Seen Jul 2026
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Sort Matrix BordersOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Validate 3x3 Digit WindowsOA · Seen Jul 2026