Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,3,1,5]
[4,3,1,3,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3282: Reach End of Array With Max Score
class Solution {
public long findMaximumScore(List<Integer> nums) {
long ans = 0;
int mx = 0;
for (int i = 0; i + 1 < nums.size(); ++i) {
mx = Math.max(mx, nums.get(i));
ans += mx;
}
return ans;
}
}
// Accepted solution for LeetCode #3282: Reach End of Array With Max Score
func findMaximumScore(nums []int) (ans int64) {
mx := 0
for _, x := range nums[:len(nums)-1] {
mx = max(mx, x)
ans += int64(mx)
}
return
}
# Accepted solution for LeetCode #3282: Reach End of Array With Max Score
class Solution:
def findMaximumScore(self, nums: List[int]) -> int:
ans = mx = 0
for x in nums[:-1]:
mx = max(mx, x)
ans += mx
return ans
// Accepted solution for LeetCode #3282: Reach End of Array With Max Score
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3282: Reach End of Array With Max Score
// class Solution {
// public long findMaximumScore(List<Integer> nums) {
// long ans = 0;
// int mx = 0;
// for (int i = 0; i + 1 < nums.size(); ++i) {
// mx = Math.max(mx, nums.get(i));
// ans += mx;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3282: Reach End of Array With Max Score
function findMaximumScore(nums: number[]): number {
let [ans, mx]: [number, number] = [0, 0];
for (const x of nums.slice(0, -1)) {
mx = Math.max(mx, x);
ans += mx;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.