From fe99be322c693b8d1b9263f3b4c5d61f9ed299e2 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 28 Dec 2024 13:03:01 +0800 Subject: [PATCH] Add solution and test-cases for problem 689 --- .../README.md | 25 ++++++------ .../Solution.go | 38 ++++++++++++++++++- .../Solution_test.go | 21 +++++----- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/README.md b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/README.md index 24fcae335..9a198b89f 100644 --- a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/README.md +++ b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/README.md @@ -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] +``` ## 结语 diff --git a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution.go b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution.go index d115ccf5e..8c3e1d0a2 100644 --- a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution.go +++ b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution.go @@ -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 } diff --git a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution_test.go b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution_test.go index 14ff50eb4..5bbe6fa73 100644 --- a/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution_test.go +++ b/leetcode/601-700/0689.Maximum-Sum-of-3-Non-Overlapping-Subarrays/Solution_test.go @@ -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() { }