14036. [GESP202609 五级 C++] 第 14 题

[GESP202609 五级 C++] 第 14 题

下面函数使用迭代方法求最大连续子段和。对于数组 {-2, 3, -1, 5, -6, 2},函数返回值是 (  )。

int maxSubArray(const vector<int> &a) {
    int best = a[0];
    int current = a[0];
    for (int i = 1; i < (int)a.size(); i++) {
        current = max(a[i], current + a[i]);
        best = max(best, current);
    }
    return best;
}

{{ select(1) }}

  • 5

  • 6

  • 7

  • 8