Skip to content

Commit b26d0cc

Browse files
authored
Merge pull request #1146 from 0xff-dev/2640
Add solution and test-cases for problem 2640
2 parents 9d856e2 + 95ff346 commit b26d0cc

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) []int64 {
4+
ans := make([]int64, len(nums))
5+
m := int64(nums[0])
6+
ans[0] = int64(nums[0]) + m
7+
for i := 1; i < len(nums); i++ {
8+
m = max(m, int64(nums[i]))
9+
ans[i] = int64(nums[i]) + m + ans[i-1]
10+
}
11+
return ans
512
}

leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 3, 7, 5, 10}, []int64{4, 10, 24, 36, 56}},
17+
{"TestCase2", []int{1, 1, 2, 4, 8, 16}, []int64{2, 4, 8, 16, 32, 64}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)