From e14587b55f4c14d0f0a49a1d752c3aedc07fa07a Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 25 Apr 2025 22:44:58 +0800 Subject: [PATCH] Add solution and test-cases for problem 2845 --- .../README.md | 54 +++++++++++++++++++ .../Solution.go | 16 ++++++ .../Solution_test.go | 39 ++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/README.md create mode 100755 leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution.go create mode 100755 leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution_test.go diff --git a/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/README.md b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/README.md new file mode 100644 index 00000000..5672d159 --- /dev/null +++ b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/README.md @@ -0,0 +1,54 @@ +# [2845.Count of Interesting Subarrays][title] + +## Description +You are given a **0-indexed** integer array `nums`, an integer `modulo`, and an integer `k`. + +Your task is to find the count of subarrays that are **interesting**. + +A **subarray** `nums[l..r]` is **interesting** if the following condition holds: + +- Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`. + +Return an integer denoting the count of interesting subarrays. + +**Note**: A subarray is a contiguous non-empty sequence of elements within an array. + +**Example 1:** + +``` +Input: nums = [3,2,4], modulo = 2, k = 1 +Output: 3 +Explanation: In this example the interesting subarrays are: +The subarray nums[0..0] which is [3]. +- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. +- Hence, cnt = 1 and cnt % modulo == k. +The subarray nums[0..1] which is [3,2]. +- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k. +- Hence, cnt = 1 and cnt % modulo == k. +The subarray nums[0..2] which is [3,2,4]. +- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. +- Hence, cnt = 1 and cnt % modulo == k. +It can be shown that there are no other interesting subarrays. So, the answer is 3. +``` + +**Example 2:** + +``` +Input: nums = [3,1,9,6], modulo = 3, k = 0 +Output: 2 +Explanation: In this example the interesting subarrays are: +The subarray nums[0..3] which is [3,1,9,6]. +- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. +- Hence, cnt = 3 and cnt % modulo == k. +The subarray nums[1..1] which is [1]. +- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. +- Hence, cnt = 0 and cnt % modulo == k. +It can be shown that there are no other interesting subarrays. So, the answer is 2. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-of-interesting-subarrays +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution.go b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution.go new file mode 100755 index 00000000..254b572e --- /dev/null +++ b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution.go @@ -0,0 +1,16 @@ +package Solution + +func Solution(nums []int, modulo int, k int) int64 { + cache := make(map[int]int) + var res int64 = 0 + cnt := 0 + cache[0] = 1 + for _, n := range nums { + if n%modulo == k { + cnt++ + } + res += int64(cache[(cnt-k+modulo)%modulo]) + cache[cnt%modulo]++ + } + return res +} diff --git a/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution_test.go b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution_test.go new file mode 100755 index 00000000..ba789bb9 --- /dev/null +++ b/leetcode/2801-2900/2845.Count-of-Interesting-Subarrays/Solution_test.go @@ -0,0 +1,39 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + inputs []int + modulo, k int + expect int64 + }{ + {"TestCase1", []int{3, 2, 4}, 2, 1, 3}, + {"TestCase2", []int{3, 1, 9, 6}, 3, 0, 2}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.inputs, c.modulo, c.k) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.inputs, c.modulo, c.k) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}