Trains the technique from
LeetCode 3629Minimum Jumps to Reach End via Prime TeleportationThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A survey robot works a straight line of valves numbered 0 to n - 1. Valve i is stamped with a pressure rating rating[i]. The robot starts at valve 0 and has to reach valve n - 1.
From valve i the robot can make either kind of move, and each move counts as one:
i - 1 or valve i + 1, when that valve exists;rating[i] is a prime number, go to any valve j whose rating is a multiple of rating[i], that is rating[j] % rating[i] == 0. Valve j may lie anywhere on the line, before or after valve i.The number 1 is not prime, so a valve rated 1 can never relay.
Return the least number of moves the robot needs to reach valve n - 1. When the line holds a single valve the robot is already there, so the answer is 0.
Example 1
Valve 0 is rated 2, which is prime, and valve 4 is rated 8, a multiple of 2, so one relay takes the robot straight to the last valve.
Example 2
Every valve is rated 1, which is not prime, so no relay is available and the robot steps from valve 0 to valve 3 one valve at a time.
Example 3
One route in four moves: step to valve 1, step to valve 2, relay from the prime rating 7 to valve 5 whose rating 21 is a multiple of 7, then step to valve 6.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fewest_moves(rating: list[int]) -> int:public int fewestMoves(int[] rating)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.