和不小于 50 的最短窗口

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

数组是 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 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论