Advanced Graphs Weighted shortest paths
482. Minimum Multiplications to Reach the End
You are given an array of positive integers arr and two numbers, start and end.
You begin with the number start. In one step you pick any element of arr and multiply your current number
by it. Then you keep only the remainder of the product divided by 100000, which is its last five digits, and
that remainder becomes your new number. You can pick the same element in as many steps as you like.
Return the fewest steps that turn start into end. If no sequence of steps does it, return -1.
Example 1
Input: arr = [5, 13, 2500], start = 48, end = 60000
2Explanation: 48 × 2500 = 120000, which leaves 20000. Then 20000 × 13 = 260000, which leaves 60000. No single step gives 60000, so the answer is 2.
Example 2
Input: arr = [2, 6], start = 4, end = 7
-1Explanation: Starting from 4, every product is even, and its last five digits are even too. So the odd number 7 can never be reached.
Example 3
Input: arr = [99999], start = 12, end = 12
0Explanation: start is already end, so no step is needed. Two steps would also bring 12 back (12 → 99988 → 12), but
zero steps is fewer.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ arr.length ≤ 1001 ≤ arr[i] ≤ 1050 ≤ start, end < 105
Clarifying questions
What you would ask an interviewer before writing any code.