Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [689.Maximum Sum of 3 Non-Overlapping Subarrays][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given an integer array `nums` and an integer `k`, find three non-overlapping subarrays of length `k` with maximum sum and return them.

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.

**Example 1:**

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

## 题意
> ...

## 题解
**EXample 2:**

### 思路1
> ...
Maximum Sum of 3 Non-Overlapping Subarrays
```go
```

Input: nums = [1,2,1,2,1,2,1,2,1], k = 2
Output: [0,2,4]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, k int) []int {
l := len(nums)
sl := l - k + 1
sum := make([]int64, sl)
cur := int64(0)
i := 0
for ; i < k; i++ {
cur += int64(nums[i])
}
sum[0] = cur
for ; i < l; i++ {
cur -= int64(nums[i-k])
cur += int64(nums[i])
sum[i-k+1] = cur
}
maxIndex := make([]int, sl)
maxIndex[sl-1] = sl - 1
for i := sl - 2; i >= 0; i-- {
maxIndex[i] = maxIndex[i+1]
if sum[i] >= sum[maxIndex[i+1]] {
maxIndex[i] = i
}
}
m := int64(0)
ans := make([]int, 3)
for i := 0; i < l; i++ {
for j := i + k; j < sl-k; j++ {
s := sum[i] + sum[j] + sum[maxIndex[j+k]]
if s > m {
ans[0], ans[1], ans[2] = i, j, maxIndex[j+k]
m = s
}
}
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 1, 2, 6, 7, 5, 1}, 2, []int{0, 3, 5}},
{"TestCase2", []int{1, 2, 1, 2, 1, 2, 1, 2, 1}, 2, []int{0, 2, 4}},
{"TestCase3", []int{4, 5, 10, 6, 11, 17, 4, 11, 1, 3}, 1, []int{4, 5, 7}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.nums, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading