Skip to content

Commit fe99be3

Browse files
committed
Add solution and test-cases for problem 689
1 parent a337ff8 commit fe99be3

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [689.Maximum Sum of 3 Non-Overlapping Subarrays][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer array `nums` and an integer `k`, find three non-overlapping subarrays of length `k` with maximum sum and return them.
5+
6+
Return the result as a list of indices representing the starting position of each interval (**0-indexed**). If there are multiple answers, return the lexicographically smallest one.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1,2,1,2,6,7,5,1], k = 2
12+
Output: [0,3,5]
13+
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
14+
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
1315
```
1416

15-
## 题意
16-
> ...
17-
18-
## 题解
17+
**EXample 2:**
1918

20-
### 思路1
21-
> ...
22-
Maximum Sum of 3 Non-Overlapping Subarrays
23-
```go
2419
```
25-
20+
Input: nums = [1,2,1,2,1,2,1,2,1], k = 2
21+
Output: [0,2,4]
22+
```
2623

2724
## 结语
2825

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) []int {
4+
l := len(nums)
5+
sl := l - k + 1
6+
sum := make([]int64, sl)
7+
cur := int64(0)
8+
i := 0
9+
for ; i < k; i++ {
10+
cur += int64(nums[i])
11+
}
12+
sum[0] = cur
13+
for ; i < l; i++ {
14+
cur -= int64(nums[i-k])
15+
cur += int64(nums[i])
16+
sum[i-k+1] = cur
17+
}
18+
maxIndex := make([]int, sl)
19+
maxIndex[sl-1] = sl - 1
20+
for i := sl - 2; i >= 0; i-- {
21+
maxIndex[i] = maxIndex[i+1]
22+
if sum[i] >= sum[maxIndex[i+1]] {
23+
maxIndex[i] = i
24+
}
25+
}
26+
m := int64(0)
27+
ans := make([]int, 3)
28+
for i := 0; i < l; i++ {
29+
for j := i + k; j < sl-k; j++ {
30+
s := sum[i] + sum[j] + sum[maxIndex[j+k]]
31+
if s > m {
32+
ans[0], ans[1], ans[2] = i, j, maxIndex[j+k]
33+
m = s
34+
}
35+
}
36+
}
37+
38+
return ans
539
}

leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 1, 2, 6, 7, 5, 1}, 2, []int{0, 3, 5}},
18+
{"TestCase2", []int{1, 2, 1, 2, 1, 2, 1, 2, 1}, 2, []int{0, 2, 4}},
19+
{"TestCase3", []int{4, 5, 10, 6, 11, 17, 4, 11, 1, 3}, 1, []int{4, 5, 7}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)