-
Notifications
You must be signed in to change notification settings - Fork 21
/
dynamic_programming.rs
51 lines (40 loc) · 1.53 KB
/
dynamic_programming.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
45
46
47
48
49
50
51
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::collections::HashMap;
impl Solution {
pub fn number_of_arithmetic_slices(a: Vec<i32>) -> i32 {
let mut cache = Vec::<HashMap<_, _>>::with_capacity(a.len());
let mut result = 0;
for num in &a {
let mut step_to_count = HashMap::with_capacity(cache.len());
for (&prev, prev_step_to_count) in a.iter().zip(&cache) {
if let Some(step) = num.checked_sub(prev) {
if let Some(prev_count) = prev_step_to_count.get(&step) {
step_to_count
.entry(step)
.and_modify(|count| *count += prev_count + 1)
.or_insert(prev_count + 1);
result += prev_count;
} else {
step_to_count.entry(step).and_modify(|count| *count += 1).or_insert(1);
}
}
}
cache.push(step_to_count);
}
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn number_of_arithmetic_slices(a: Vec<i32>) -> i32 {
Self::number_of_arithmetic_slices(a)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}