Maximum Array Hopping Score I - 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 an index 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,5,3,9,2]
Output: 29
💡 Note: Optimal path: 0→3→4. Score = (3-0)*9 + (4-3)*2 = 27 + 2 = 29
Example 2 — Minimum Array
$ Input: nums = [5,2]
Output: 2
💡 Note: Only one jump possible: 0→1. Score = (1-0)*2 = 2
Example 3 — Sequential Jumps
$ Input: nums = [1,2,3,4]
Output: 12
💡 Note: Best path: 0→3. Score = (3-0)*4 = 12. Better than 0→1→2→3 which gives 8

Constraints

  • 2 ≤ nums.length ≤ 1000
  • -103 ≤ nums[i] ≤ 103
  • You must start from index 0 and reach the last index

Visualization

Tap to expand
Maximum Array Hopping Score I INPUT Array: nums 0 1 2 3 4 1 5 3 9 2 Hopping Rules: Start at index 0 Jump to any j greater than i Score = (j - i) * nums[j] Reach last element Score Formula Example: Hop 0 to 3: (3-0)*9 = 27 Hop 3 to 4: (4-3)*2 = 2 Total: 27 + 2 = 29 ALGORITHM STEPS 1 Track Maximum Suffix For each position, find max value to the right 2 Greedy Jump Strategy Always jump to position with maximum value ahead 3 Calculate Score Score += (j - i) * nums[j] for each optimal hop 4 Sum All Scores Accumulate scores until reaching last element Optimal Path Trace: i=0: max ahead=9 at j=3 Jump 0 to 3: score=27 i=3: must reach j=4 Jump 3 to 4: score=2 FINAL RESULT Optimal Hopping Path: 1 i=0 5 skip 3 skip 9 i=3 2 i=4 Score Calculation: Hop 1: (3-0)*9 = 27 Hop 2: (4-3)*2 = 2 Total: 29 Output: 29 OK - Maximum Score Found! Key Insight: The optimal strategy is greedy: always jump to the position with the maximum value ahead. Since score = (distance) * (value), maximizing the target value while covering distance gives optimal results. Time Complexity: O(n) with suffix maximum preprocessing. Space Complexity: O(1) or O(n) for suffix array. TutorialsPoint - Maximum Array Hopping Score I | Optimal Greedy Approach
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 5
23.4K Views
Medium Frequency
~25 min Avg. Time
847 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