和不小于 50 的最短窗口
数组是 17 8 15 13 23 24 19。运行下面这段程序:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int min_len(const vector<int>& a, int target) {
int lo = 0;
int s = 0;
int best = (int)a.size() + 1;
for (int hi = 0; hi < (int)a.size(); hi++) {
s += a[hi];
while (s >= target) {
best = min(best, hi - lo + 1);
s -= a[lo];
lo++;
}
}
return best;
}
int main() {
cout << min_len({17, 8, 15, 13, 23, 24, 19}, 50) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)