Watch 10 video solutions for Reach End of Array With Max Score, a medium level problem involving Array, Greedy. This walkthrough by codi has 1,384 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums of length n.
Your goal is to start at index 0 and reach index n - 1. You can only jump to indices greater than your current index.
The score for a jump from index i to index j is calculated as (j - i) * nums[i].
Return the maximum possible total score by the time you reach the last index.
Example 1:
Input: nums = [1,3,1,5]
Output: 7
Explanation:
First, jump to index 1 and then jump to the last index. The final score is 1 * 1 + 2 * 3 = 7.
Example 2:
Input: nums = [4,3,1,3,2]
Output: 16
Explanation:
Jump directly to the last index. The final score is 4 * 4 = 16.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem Overview: You start at index 0 of an array and want to reach the last index. From index i you can jump to any j > i, gaining score based on the value at nums[i] and the distance traveled. The goal is to choose jumps that maximize the total score by the time you reach the end.
Approach 1: Dynamic Programming with Priority Queue (O(n log n) time, O(n) space)
This method treats the problem as a DP transition. Let dp[i] represent the best score achievable when reaching index i. For each new index, you consider previous indices and compute the best transition using a priority queue that keeps candidates ordered by their contribution to future jumps. Each step pushes potential transitions and removes weaker ones as you move forward. This works well conceptually but adds log n overhead due to heap operations.
Approach 2: Dynamic Programming with Maximum Score Tracking (O(n) time, O(n) space)
The key observation is that every transition depends on the best prior value that can contribute to the next position. Instead of evaluating all previous indices, track the best candidate while scanning from left to right. Maintain the maximum achievable contribution so far and update dp[i] using that value. This eliminates repeated comparisons and reduces the complexity to linear time.
Approach 3: Greedy Approach with Optimization (O(n) time, O(1) space)
A deeper observation simplifies the DP even further. While moving across the array, the optimal move at each step always depends on the maximum nums[i] encountered so far. Keep a running maximum and add it to the total score as you advance one index at a time. Conceptually, this simulates extending the best previous jump across the next position. The greedy approach removes the DP array entirely and runs in constant space. This technique appears often in greedy problems on arrays.
Approach 4: Optimized DP with Monotonic Deque (O(n) time, O(n) space)
A monotonic deque can maintain candidate indices whose contributions remain competitive for future transitions. As you iterate through the array, remove indices that produce worse scores than the current one and push the new index while maintaining order. The front of the deque always provides the best transition for the current position. This pattern is common in optimized dynamic programming problems where previous states dominate others.
Recommended for interviews: The greedy O(n) approach is typically what interviewers expect after discussing the DP formulation. Showing the DP reasoning demonstrates understanding of the state transition, while recognizing the prefix maximum optimization proves you can simplify the solution to linear time and constant space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DP with Priority Queue | O(n log n) | O(n) | When implementing a straightforward DP transition and optimizing candidate selection with a heap |
| DP with Maximum Score Tracking | O(n) | O(n) | General DP solution that removes the heap but still keeps an explicit DP array |
| Greedy Optimization | O(n) | O(1) | Best practical solution when the optimal contribution comes from the maximum prefix value |
| DP with Monotonic Deque | O(n) | O(n) | Useful when maintaining a set of dominant previous states for future transitions |