#2640
Medium Algorithms Find the score of all prefixes of an array
Array Prefix Sum
72.8% acceptance
Feb 25, 2026
364
48
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].
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn find_prefix_score(nums: Vec<i32>) -> Vec<i64> {
let n = nums.len();
let mut ans = vec![0i64; n];
let mut running_max = 0i64;
let mut running_score = 0i64;
for i in 0..n {
let v = nums[i] as i64;
if v > running_max {
running_max = v;
}
running_score += v + running_max;
ans[i] = running_score;
}
ans
}
}