Reach End of Array With Max Score - Problem
Array Jumping Challenge: Maximize Your Score!
You're given an integer array
๐ฏ Scoring System: Each jump from index
Goal: Find the maximum possible total score you can achieve by the time you reach the final index.
Example: If
You're given an integer array
nums of length n, and you need to traverse from the first index (0) to the last index (n-1) by making strategic jumps. Here's the twist: you can only jump forward to indices greater than your current position!๐ฏ Scoring System: Each jump from index
i to index j earns you (j - i) ร nums[i] points. The longer your jump and the higher the value at your starting position, the more points you earn!Goal: Find the maximum possible total score you can achieve by the time you reach the final index.
Example: If
nums = [1, 3, 1, 5], jumping from index 1 (value=3) to index 3 (value=5) would score (3-1) ร 3 = 6 points. Input & Output
example_1.py โ Basic Case
$
Input:
nums = [1, 3, 1, 5]
โบ
Output:
7
๐ก Note:
Optimal path: 0โ1 (score: 1ร1=1), then 1โ3 (score: 2ร3=6). Total: 1+6=7. This beats jumping 0โ2โ3 (1ร2 + 1ร1 = 3) or 0โ3 directly (3ร1 = 3).
example_2.py โ Decreasing Array
$
Input:
nums = [5, 3, 2, 1]
โบ
Output:
15
๐ก Note:
Since values only decrease, we jump directly from start to end: (3-0) ร 5 = 15 points. No benefit in stopping at intermediate positions.
example_3.py โ Single Element
$
Input:
nums = [7]
โบ
Output:
0
๐ก Note:
With only one element, we're already at the end, so no jumps are needed and score is 0.
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 105
- You must start at index 0 and reach index n-1
- You can only jump to indices greater than your current index
Visualization
Tap to expand
Understanding the Visualization
1
Scan Forward
From current position, look for the next platform with a higher value
2
Strategic Jump
Jump to that higher-value platform to maximize future scoring potential
3
Calculate Score
Add (jump_distance ร starting_platform_value) to total score
4
Repeat or Finish
Continue strategy or jump directly to end if no higher values remain
Key Takeaway
๐ฏ Key Insight: The greedy approach works because higher starting values multiply distance gains, so we should always prioritize reaching and staying at high-value positions before making longer jumps.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code