Skip to content

Commit

Permalink
Merge pull request #1037 from 0xff-dev/2461
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2461
  • Loading branch information
6boris authored Dec 24, 2024
2 parents a3098ff + 90d3ebd commit 7fca019
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [2461.Maximum Sum of Distinct Subarrays With Length K][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
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:

- The length of the subarray is `k`, and
- All the elements of the subarray are **distinct**.

Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return `0`.

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Maximum Sum of Distinct Subarrays With Length K
```go
```

Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(nums []int, k int) int64 {
l := len(nums)
count := make(map[int]int)
cur := int64(0)
for i := 0; i < k; i++ {
count[nums[i]]++
cur += int64(nums[i])
}

ans := int64(0)
if len(count) == k {
ans = cur
}

// [1, 2], 3, 4,
s, e := 0, k
for ; e < l; s, e = s+1, e+1 {
count[nums[s]]--
cur -= int64(nums[s])
if count[nums[s]] == 0 {
delete(count, nums[s])
}
count[nums[e]]++
cur += int64(nums[e])
if len(count) == k {
ans = max(ans, cur)
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
nums []int
k int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 5, 4, 2, 9, 9, 9}, 3, 15},
{"TestCase2", []int{4, 4, 4}, 3, 0},
}

// 开始测试
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() {
}

0 comments on commit 7fca019

Please sign in to comment.