Max-score jumps with primes ending in 3
You are given a non-empty integer array nums of length n. You stand on index 0 and must finish on index n - 1.
From index i you may move forward in one of two ways:
-
Step: move to
i + 1. -
Prime jump: move to
i + p, wherepis a prime number whose last digit is3(3, 13, 23, 43, 53, 73, 83, ...).
You may never move past the end of the array. Your score is the sum of nums[j] over every index j you land on, including index 0 and index n - 1.
Return the largest score you can get.
Implement:
def max_jump_score(nums: list[int]) -> int: ...
Constraints
1 <= n <= 2 * 10^4-10^4 <= nums[i] <= 10^4
Example 1
nums = [2, -50, -50, 6]
output: 8
Jump 3 from index 0 to index 3: 2 + 6 = 8.
Example 2
nums = [1, -9, -9, 2, 2, -9, -9, 3, -9, -9, 4]
output: 12
Path 0 -> 3 -> 4 -> 7 -> 10 (jump 3, step, jump 3, jump 3): 1 + 2 + 2 + 3 + 4 = 12.
A likely follow-up: also return the indices of one optimal path.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
