TypeScript Min Avg Two Slice
function minAvgTwoSlice(a: number[]): number {
let idx = 0;
let min = (a[0] + a[1]) / 2;
for (let i = 0; i < a.length - 1; i++) {
let cur = (a[i] + a[i + 1]) / 2;
if (a[i + 2] !== undefined) {
const three = (a[i] + a[i + 1] + a[i + 2]) / 3;
cur = cur < three ? cur : three;
}
if (cur < min) {
min = cur;
idx = i;
}
}
return idx;
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.