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.
We define the conversion array conver of an array arr as follows:
conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.We also define the score of an array arr as the sum of the values of the conversion array of arr.
Given a 0-indexed integer array nums of length n, return an array ans of length n where ans[i] is the score of the prefix nums[0..i].
Example 1:
Input: nums = [2,3,7,5,10] Output: [4,10,24,36,56] Explanation: For the prefix [2], the conversion array is [4] hence the score is 4 For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10 For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24 For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36 For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
Example 2:
Input: nums = [1,1,2,4,8,16] Output: [2,4,8,16,32,64] Explanation: For the prefix [1], the conversion array is [2] hence the score is 2 For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4 For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8 For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16 For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32 For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: We define the conversion array conver of an array arr as follows: conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i. We also define the score of an array arr as the sum of the values of the conversion array of arr. Given a 0-indexed integer array nums of length n, return an array ans of length n where ans[i] is the score of the prefix nums[0..i].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,3,7,5,10]
[1,1,2,4,8,16]
most-beautiful-item-for-each-query)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2640: Find the Score of All Prefixes of an Array
class Solution {
public long[] findPrefixScore(int[] nums) {
int n = nums.length;
long[] ans = new long[n];
int mx = 0;
for (int i = 0; i < n; ++i) {
mx = Math.max(mx, nums[i]);
ans[i] = nums[i] + mx + (i == 0 ? 0 : ans[i - 1]);
}
return ans;
}
}
// Accepted solution for LeetCode #2640: Find the Score of All Prefixes of an Array
func findPrefixScore(nums []int) []int64 {
n := len(nums)
ans := make([]int64, n)
mx := 0
for i, x := range nums {
mx = max(mx, x)
ans[i] = int64(x + mx)
if i > 0 {
ans[i] += ans[i-1]
}
}
return ans
}
# Accepted solution for LeetCode #2640: Find the Score of All Prefixes of an Array
class Solution:
def findPrefixScore(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n
mx = 0
for i, x in enumerate(nums):
mx = max(mx, x)
ans[i] = x + mx + (0 if i == 0 else ans[i - 1])
return ans
// Accepted solution for LeetCode #2640: Find the Score of All Prefixes of an Array
fn find_prefix_score(nums: Vec<i32>) -> Vec<i64> {
let mut max = 0i64;
let mut acc = 0i64;
let mut ret: Vec<i64> = vec![];
for num in nums {
let num = num as i64;
max = std::cmp::max(max, num);
acc += max + num;
ret.push(acc);
}
ret
}
fn main() {
let nums = vec![2, 3, 7, 5, 10];
let ret = find_prefix_score(nums);
println!("ret={ret:?}");
}
#[test]
fn test_find_prefix_score() {
{
let nums = vec![2, 3, 7, 5, 10];
let expected = vec![4i64, 10, 24, 36, 56];
let ret = find_prefix_score(nums);
assert_eq!(ret, expected);
}
{
let nums = vec![1, 1, 2, 4, 8, 16];
let expected = vec![2, 4, 8, 16, 32, 64i64];
let ret = find_prefix_score(nums);
assert_eq!(ret, expected);
}
}
// Accepted solution for LeetCode #2640: Find the Score of All Prefixes of an Array
function findPrefixScore(nums: number[]): number[] {
const n = nums.length;
const ans: number[] = new Array(n);
let mx: number = 0;
for (let i = 0; i < n; ++i) {
mx = Math.max(mx, nums[i]);
ans[i] = nums[i] + mx + (i === 0 ? 0 : ans[i - 1]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.