You are given an integer array nums of length n. A token starts on index 0 and must reach index n - 1.
From an index i, the token may move in one of two ways:
Unit step: move to index i + 1.
Prime jump: move to index i + p, where p is a prime number whose last digit is 3 (that is, p is one of 3, 13, 23, 43, 53, 73, ...) and i + p < n.
Each time the token lands on an index, including the starting index 0 and the final index n - 1, it collects the value stored there. Return the maximum total value the token can collect over all valid paths from index 0 to index n - 1.
Because a unit step is always available, index n - 1 is always reachable.
Examples
Example 1
Input: nums = [10,-5,-10,20,10]
Output: 40
Explanation: The only usable prime ending in 3 that is at most 4 is 3.
Prime-jump from index 0 to index 3 (collecting 10, then 20), then a unit step
from index 3 to index 4 (collecting 10). Total = 10 + 20 + 10 = 40.
Example 2
Input: nums = [10,-100,-100,10,-100,-100,10]
Output: 30
Explanation: Two prime jumps of length 3 give the path 0 -> 3 -> 6, collecting
10 + 10 + 10 = 30. Every unit-step path is forced through the -100 values, so
jumping is strictly better.
Constraints
1≤nums.length≤104
−104≤nums[i]≤104
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up