Maximum Array Hopping Score II - Problem

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

In each hop, you can jump from index i to any index j where j > i, and you get a score of (j - i) * nums[j].

Return the maximum score you can get.

Input & Output

Example 1 — Basic Hopping
$ Input: nums = [1,3,6,4,5]
Output: 22
💡 Note: Optimal path: 0→2→4. Jump from index 0 to 2: (2-0)*6=12. Jump from index 2 to 4: (4-2)*5=10. Total score: 12+10=22.
Example 2 — Simple Case
$ Input: nums = [2,5,3]
Output: 8
💡 Note: We can jump 0→1→2 for score (1-0)*5+(2-1)*3 = 5+3 = 8, or jump 0→2 directly for score (2-0)*3 = 6. The maximum is 8.
Example 3 — Two Elements
$ Input: nums = [7,2]
Output: 2
💡 Note: Only one jump possible: from index 0 to index 1, score = (1-0)*2 = 2.

Constraints

  • 2 ≤ nums.length ≤ 1000
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Maximum Array Hopping Score II INPUT Array nums[] i=0 i=1 i=2 i=3 i=4 1 3 6 4 5 START END Score Formula: (j - i) * nums[j] Possible Hops from i=0: 0-->1: (1-0)*3 = 3 0-->2: (2-0)*6 = 12 0-->3: (3-0)*4 = 12 0-->4: (4-0)*5 = 20 ALGORITHM (DP) 1 Initialize DP Array dp[i] = max score to reach i 2 For each position j Check all previous i < j 3 Update dp[j] dp[j] = max(dp[i] + score) 4 Return dp[n-1] Max score at last index DP Table Computation: idx val dp[i] 0 1 0 1 3 3 2 6 12 3 4 12 4 5 16 FINAL RESULT Optimal Hopping Path: 1 i=0 3 6 i=2 4 5 i=4 Score Calculation: Hop 0 --> 2: (2-0) * 6 = 12 Hop 2 --> 4: (4-2) * 5 = 10 Total: 12 + 10 = 22 (But DP finds better: 0-->2-->4) Maximum Score: 16 Key Insight: DP optimization: Track maximum value of (dp[i] - i*nums[j]) for all previous positions. For j: score = max_prev + j*nums[j], where max_prev = max(dp[i] - i*nums[j]) for i < j. This reduces time complexity from O(n^2) to O(n) by maintaining running maximum. TutorialsPoint - Maximum Array Hopping Score II | Dynamic Programming Approach
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen