C++ Min Avg Two Slice
#include <algorithm>
#include <vector>
int minAvgTwoSlice(const std::vector<int>& a)
{
int idx = 0;
double min = (a[0] + a[1]) / 2.0;
for (std::size_t i = 0; i + 1 < a.size(); ++i) {
double cur = (a[i] + a[i + 1]) / 2.0;
if (i + 2 < a.size()) {
double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
cur = std::min(cur, three);
}
if (cur < min) {
min = cur;
idx = static_cast<int>(i);
}
}
return idx;
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.