Rust Min Avg Two Slice
fn min_avg_two_slice(a: &[i64]) -> i64 {
let mut idx = 0i64;
let mut min = (a[0] + a[1]) as f64 / 2.0;
for i in 0..a.len() - 1 {
let mut cur = (a[i] + a[i + 1]) as f64 / 2.0;
if i + 2 < a.len() {
let three = (a[i] + a[i + 1] + a[i + 2]) as f64 / 3.0;
cur = cur.min(three);
}
if cur < min {
min = cur;
idx = i as i64;
}
}
idx
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.