题解 | #求连续子数组的最大和# 前缀和

求连续子数组的最大和

https://www.nowcoder.com/practice/8705437354604a7cb9ba7bf959e42de6

#include <iostream>
#include <vector>

using namespace std;
vector<int> a;

int main() {
    int x = 0;
    char c;
    int p = 1;
    while((c=getchar()) != '\n'){
        if(c == '-'){
            p = -1;
        }else if(c == ','){
            a.push_back(x*p);
            p = 1;
            x = 0;
        }else{
            x = x*10+(c-'0');
        }
    }
    a.push_back(x*p);
    a.insert(a.begin(), 0);

    int n = a.size();
    vector<int> pre(n+1,0);

    for(int i = 1; i <= n; i++){
        pre[i] = pre[i-1]+a[i];

    }
    int res = 0;
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            res = max(res ,pre[j]-pre[i-1] > 0 ? pre[j]-pre[i-1] : 0);
        }
    }
    cout << res << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

#每日一题#
全部评论

相关推荐

风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务