From 90d3ebdada8313eb5d05d53a1fe2db8d2dc37b13 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 19 Nov 2024 08:59:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 2461 --- .../README.md | 38 ++++++++++++------- .../Solution.go | 31 ++++++++++++++- .../Solution_test.go | 20 +++++----- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/README.md b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/README.md index 8a47ee375..57d130c26 100755 --- a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/README.md +++ b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution.go b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution.go index d115ccf5e..d3599667b 100644 --- a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution.go +++ b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution.go @@ -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 } diff --git a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution_test.go b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution_test.go index 14ff50eb4..8f70ab3fa 100644 --- a/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution_test.go +++ b/leetcode/2401-2500/2461.Maximum-Sum-of-Distinct-Subarrays-With-Length-K/Solution_test.go @@ -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() { }