Skip to content

Commit 58c361f

Browse files
authored
Update README.md
1 parent 3c1f76c commit 58c361f

File tree

1 file changed

+29
-0
lines changed
  • solution/2800-2899/2845.Count of Interesting Subarrays

1 file changed

+29
-0
lines changed

solution/2800-2899/2845.Count of Interesting Subarrays/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,35 @@ function countInterestingSubarrays(nums: number[], modulo: number, k: number): n
208208
}
209209
```
210210

211+
#### Rust
212+
213+
```rust
214+
use std::collections::HashMap;
215+
216+
impl Solution {
217+
pub fn count_interesting_subarrays(nums: Vec<i32>, modulo: i32, k: i32) -> i64 {
218+
let mut arr: Vec<i32> = nums
219+
.iter()
220+
.map(|&x| if x % modulo == k { 1 } else { 0 })
221+
.collect();
222+
let mut cnt: HashMap<i32, i64> = HashMap::new();
223+
cnt.insert(0, 1);
224+
225+
let mut ans: i64 = 0;
226+
let mut s: i32 = 0;
227+
228+
for x in arr {
229+
s += x;
230+
let key = (s - k).rem_euclid(modulo);
231+
ans += *cnt.get(&key).unwrap_or(&0);
232+
*cnt.entry(s % modulo).or_insert(0) += 1;
233+
}
234+
235+
ans
236+
}
237+
}
238+
```
239+
211240
<!-- tabs:end -->
212241

213242
<!-- solution:end -->

0 commit comments

Comments
 (0)