|
| 1 | +# [2640.Find the Score of All Prefixes of an Array][title] |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +We define the **conversion array** `conver` of an array `arr` as follows: |
| 6 | + |
| 7 | +- `conver[i] = arr[i] + max(arr[0..i])` where `max(arr[0..i])` is the maximum value of `arr[j]` over `0 <= j <= i`. |
| 8 | + |
| 9 | +We also define the **score** of an array `arr` as the sum of the values of the conversion array of `arr`. |
| 10 | + |
| 11 | +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]`. |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | + |
| 15 | +``` |
| 16 | +Input: nums = [2,3,7,5,10] |
| 17 | +Output: [4,10,24,36,56] |
| 18 | +Explanation: |
| 19 | +For the prefix [2], the conversion array is [4] hence the score is 4 |
| 20 | +For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10 |
| 21 | +For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24 |
| 22 | +For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36 |
| 23 | +For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56 |
| 24 | +``` |
| 25 | + |
| 26 | +**EXample 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: nums = [1,1,2,4,8,16] |
| 30 | +Output: [2,4,8,16,32,64] |
| 31 | +Explanation: |
| 32 | +For the prefix [1], the conversion array is [2] hence the score is 2 |
| 33 | +For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4 |
| 34 | +For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8 |
| 35 | +For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16 |
| 36 | +For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32 |
| 37 | +For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64 |
| 38 | +``` |
| 39 | + |
| 40 | +## 结语 |
| 41 | + |
| 42 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] |
| 43 | + |
| 44 | +[title]: https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array |
| 45 | +[me]: https://github.com/kylesliu/awesome-golang-algorithm |
0 commit comments