Skip to content

Commit e14587b

Browse files
committed
Add solution and test-cases for problem 2845
1 parent a01e89c commit e14587b

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [2845.Count of Interesting Subarrays][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums`, an integer `modulo`, and an integer `k`.
5+
6+
Your task is to find the count of subarrays that are **interesting**.
7+
8+
A **subarray** `nums[l..r]` is **interesting** if the following condition holds:
9+
10+
- Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`.
11+
12+
Return an integer denoting the count of interesting subarrays.
13+
14+
**Note**: A subarray is a contiguous non-empty sequence of elements within an array.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: nums = [3,2,4], modulo = 2, k = 1
20+
Output: 3
21+
Explanation: In this example the interesting subarrays are:
22+
The subarray nums[0..0] which is [3].
23+
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
24+
- Hence, cnt = 1 and cnt % modulo == k.
25+
The subarray nums[0..1] which is [3,2].
26+
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
27+
- Hence, cnt = 1 and cnt % modulo == k.
28+
The subarray nums[0..2] which is [3,2,4].
29+
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
30+
- Hence, cnt = 1 and cnt % modulo == k.
31+
It can be shown that there are no other interesting subarrays. So, the answer is 3.
32+
```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: nums = [3,1,9,6], modulo = 3, k = 0
38+
Output: 2
39+
Explanation: In this example the interesting subarrays are:
40+
The subarray nums[0..3] which is [3,1,9,6].
41+
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
42+
- Hence, cnt = 3 and cnt % modulo == k.
43+
The subarray nums[1..1] which is [1].
44+
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
45+
- Hence, cnt = 0 and cnt % modulo == k.
46+
It can be shown that there are no other interesting subarrays. So, the answer is 2.
47+
```
48+
49+
## 结语
50+
51+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
52+
53+
[title]: https://leetcode.com/problems/count-of-interesting-subarrays
54+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Solution
2+
3+
func Solution(nums []int, modulo int, k int) int64 {
4+
cache := make(map[int]int)
5+
var res int64 = 0
6+
cnt := 0
7+
cache[0] = 1
8+
for _, n := range nums {
9+
if n%modulo == k {
10+
cnt++
11+
}
12+
res += int64(cache[(cnt-k+modulo)%modulo])
13+
cache[cnt%modulo]++
14+
}
15+
return res
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs []int
14+
modulo, k int
15+
expect int64
16+
}{
17+
{"TestCase1", []int{3, 2, 4}, 2, 1, 3},
18+
{"TestCase2", []int{3, 1, 9, 6}, 3, 0, 2},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.inputs, c.modulo, c.k)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.inputs, c.modulo, c.k)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
}
36+
37+
// 使用案列
38+
func ExampleSolution() {
39+
}

0 commit comments

Comments
 (0)