Took me longer than it should have to see the structure.
Recognize this as a dynamic programming problem where the maximum score to reach index i depends on the best score from index i-1 or any previous index j where arr[j] % 10 == 3. Compute dp[i] = arr[i] + max(dp[i-1], max_{j < i, arr[j]%10==3} dp[j]), and optimize the second term by maintaining the running maximum of dp[j] for indices with value ending in 3.
Pro tip: Clarify edge cases upfront: if the array has only one element, the score is just that element; also confirm whether you must land on the last index (yes) and whether you can skip indices (only via the jump rule). This shows attention to detail and prevents wrong assumptions.
Let dp[i] be the maximum score to reach index i. The answer will be dp[n-1] where n is the array length.
Base: dp[0] = arr[0]. Transition: dp[i] = arr[i] + max(dp[i-1], best_so_far) where best_so_far is the maximum dp[j] for j < i with arr[j] % 10 == 3.
Maintain a variable best_so_far that stores the maximum dp[j] seen so far for indices j where arr[j] % 10 == 3. Update it after computing each dp[i] if arr[i] % 10 == 3.
Iterate from i = 1 to n-1, compute dp[i] using the transition, and update best_so_far accordingly. Return dp[n-1].
Time complexity O(n), space O(1) if we only keep the previous dp and best_so_far. Handle n=1, negative numbers, and ensure best_so_far is initialized to -infinity if no valid jump source exists.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.