Skip to content

Commit 49c3330

Browse files
authored
Merge pull request #1383 from 0xff-dev/3381
Add solution and test-cases for problem 3381
2 parents 86e4619 + 50fa166 commit 49c3330

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

leetcode/3301-3400/3381.Maximum-Subarray-Sum-With-Length-Divisible-by-K/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3381.Maximum Subarray Sum With Length Divisible by K][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+
You are given an array of integers `nums` and an integer `k`.
5+
6+
Return the **maximum** sum of a subarray of `nums`, such that the size of the subarray is **divisible** by `k`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1,2], k = 1
12+
13+
Output: 3
14+
15+
Explanation:
16+
17+
The subarray [1, 2] with sum 3 has length equal to 2 which is divisible by 1.
18+
```
19+
20+
**Example 2:**
21+
1322
```
23+
Input: nums = [-1,-2,-3,-4,-5], k = 4
24+
25+
Output: -10
1426
15-
## 题意
16-
> ...
27+
Explanation:
28+
29+
The maximum sum subarray is [-1, -2, -3, -4] which has length equal to 4 which is divisible by 4.
30+
```
1731

18-
## 题解
32+
**Example 3:**
1933

20-
### 思路1
21-
> ...
22-
Maximum Subarray Sum With Length Divisible by K
23-
```go
2434
```
35+
Input: nums = [-5,1,2,-3,4], k = 2
2536
37+
Output: 4
38+
39+
Explanation:
40+
41+
The maximum sum subarray is [1, 2, -3, 4] which has length equal to 4 which is divisible by 2.
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func Solution(nums []int, k int) int64 {
6+
n := len(nums)
7+
prefixSum := int64(0)
8+
maxSum := int64(math.MinInt64)
9+
kSum := make([]int64, k)
10+
for i := range k {
11+
kSum[i] = math.MaxInt64 / 2
12+
}
13+
kSum[k-1] = 0
14+
for i := range n {
15+
prefixSum += int64(nums[i])
16+
if prefixSum-kSum[i%k] > maxSum {
17+
maxSum = prefixSum - kSum[i%k]
18+
}
19+
if prefixSum < kSum[i%k] {
20+
kSum[i%k] = prefixSum
21+
}
22+
}
23+
return maxSum
524
}

leetcode/3301-3400/3381.Maximum-Subarray-Sum-With-Length-Divisible-by-K/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+
inputs []int
14+
k int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2}, 1, 3},
18+
{"TestCase2", []int{-1, -2, -3, -4, -5}, 4, -10},
19+
{"TestCase3", []int{-5, 1, 2, -3, 4}, 2, 4},
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.inputs, 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.inputs, 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)