Find the Score of All Prefixes of an Array - Problem
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].
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,1,3,4]
›
Output:
[4,7,13,21]
💡 Note:
For prefix [2]: max=2, conversion=[2+2]=[4], score=4. For prefix [2,1]: conversion=[2+2,1+2]=[4,3], score=7. For prefix [2,1,3]: conversion=[2+2,1+2,3+3]=[4,3,6], score=13. For prefix [2,1,3,4]: conversion=[2+2,1+2,3+3,4+4]=[4,3,6,8], score=21.
Example 2 — Increasing Array
$
Input:
nums = [1,3,6,4,5]
›
Output:
[2,8,20,28,38]
💡 Note:
For prefix [1]: conversion=[2], score=2. For prefix [1,3]: conversion=[2,6], score=8. For prefix [1,3,6]: conversion=[2,6,12], score=20. Each element gets added to the running maximum at its position.
Example 3 — Decreasing Array
$
Input:
nums = [5,4,3,2,1]
›
Output:
[10,19,27,34,40]
💡 Note:
Maximum stays at 5 throughout, so conversion array for each prefix: [5]→[10], [5,4]→[10,9], [5,4,3]→[10,9,8], etc. Prefix scores are cumulative sums of conversion arrays.
Constraints
- 1 ≤ nums.length ≤ 105
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code