Maximum Array Hopping Score II - Problem

You're given an array of integers nums and need to find the maximum score by hopping through the array from start to finish. Starting at index 0, you can jump to any index j where j > i (current position).

Each jump from index i to index j gives you a score of (j - i) * nums[j]. The longer the jump and the larger the destination value, the higher your score!

Goal: Find the maximum total score you can achieve by making optimal jumps until you reach the last element.

Example: For array [1, 5, 2, 3], jumping from index 0 directly to index 1 gives score (1-0) * 5 = 5, then from index 1 to index 3 gives score (3-1) * 3 = 6, for a total of 11.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 5, 2, 3]
โ€บ Output: 11
๐Ÿ’ก Note: Optimal path: 0โ†’1โ†’3. Jump from index 0 to 1 gives score (1-0)*5=5. Jump from index 1 to 3 gives score (3-1)*3=6. Total: 5+6=11.
example_2.py โ€” Long Jump Better
$ Input: [1, 2, 3, 10]
โ€บ Output: 30
๐Ÿ’ก Note: Better to jump directly from index 0 to index 3: (3-0)*10=30, rather than making smaller jumps with lower total score.
example_3.py โ€” Minimum Array
$ Input: [5, 2]
โ€บ Output: 2
๐Ÿ’ก Note: Only one possible jump from index 0 to 1: (1-0)*2=2.

Visualization

Tap to expand
Mountain Hopping StrategyStartGoalOptimal jump targets (skyline)Blocked by taller peaksCurrent position
Understanding the Visualization
1
Survey the landscape
Look at all peaks ahead and identify which ones are worth considering
2
Build the skyline
Only peaks that aren't blocked by taller ones closer to you matter
3
Plan optimal jumps
For each position, calculate the best score among all skyline peaks
4
Update the skyline
As you process each position, update which peaks remain optimal
Key Takeaway
๐ŸŽฏ Key Insight: Like a smart mountain climber, we only need to consider peaks in our 'line of sight' - those that aren't blocked by taller peaks closer to us. This skyline approach reduces our search space dramatically!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is pushed and popped from stack at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack can contain up to n elements in worst case

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • You must start at index 0 and can only jump forward
  • You must reach the last index of the array
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
58.0K Views
High Frequency
~25 min Avg. Time
1.3K 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