Skip to content

Commit 7fca019

Browse files
authored
Merge pull request #1037 from 0xff-dev/2461
Add solution and test-cases for problem 2461
2 parents a3098ff + 90d3ebd commit 7fca019

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2461.Maximum Sum of Distinct Subarrays With Length 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 integer array `nums` and an integer `k`. Find the maximum subarray sum of all the subarrays of `nums` that meet the following conditions:
5+
6+
- The length of the subarray is `k`, and
7+
- All the elements of the subarray are **distinct**.
8+
9+
Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return `0`.
10+
11+
A **subarray** is a contiguous non-empty sequence of elements within an array.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [1,5,4,2,9,9,9], k = 3
17+
Output: 15
18+
Explanation: The subarrays of nums with length 3 are:
19+
- [1,5,4] which meets the requirements and has a sum of 10.
20+
- [5,4,2] which meets the requirements and has a sum of 11.
21+
- [4,2,9] which meets the requirements and has a sum of 15.
22+
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
23+
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
24+
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Sum of Distinct Subarrays With Length K
23-
```go
2429
```
25-
30+
Input: nums = [4,4,4], k = 3
31+
Output: 0
32+
Explanation: The subarrays of nums with length 3 are:
33+
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
34+
We return 0 because no subarrays meet the conditions.
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int64 {
4+
l := len(nums)
5+
count := make(map[int]int)
6+
cur := int64(0)
7+
for i := 0; i < k; i++ {
8+
count[nums[i]]++
9+
cur += int64(nums[i])
10+
}
11+
12+
ans := int64(0)
13+
if len(count) == k {
14+
ans = cur
15+
}
16+
17+
// [1, 2], 3, 4,
18+
s, e := 0, k
19+
for ; e < l; s, e = s+1, e+1 {
20+
count[nums[s]]--
21+
cur -= int64(nums[s])
22+
if count[nums[s]] == 0 {
23+
delete(count, nums[s])
24+
}
25+
count[nums[e]]++
26+
cur += int64(nums[e])
27+
if len(count) == k {
28+
ans = max(ans, cur)
29+
}
30+
}
31+
return ans
532
}

leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ 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 int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 5, 4, 2, 9, 9, 9}, 3, 15},
18+
{"TestCase2", []int{4, 4, 4}, 3, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)