-
Notifications
You must be signed in to change notification settings - Fork 21
/
iterative.rs
44 lines (33 loc) · 1.1 KB
/
iterative.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::collections::HashMap;
impl Solution {
pub fn subarrays_div_by_k(nums: Vec<i32>, k: i32) -> i32 {
let mut counts = HashMap::from([(0_u32, 1_i32)]);
let mut result = 0;
let mut sum = 0;
assert_ne!(k, 0);
for num in nums {
sum += num.rem_euclid(k) as u32;
sum = sum.checked_sub(k as _).unwrap_or(sum);
if let Some(&count) = counts.get(&sum) {
result += count;
}
counts.entry(sum).and_modify(|count| *count += 1).or_insert(1);
}
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn subarrays_div_by_k(nums: Vec<i32>, k: i32) -> i32 {
Self::subarrays_div_by_k(nums, k)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}