题解 | 最大序列和

最大序列和

https://www.nowcoder.com/practice/df219d60a7af4171a981ef56bd597f7b

#include <iostream>
using namespace std;

const int N = 1000010;
typedef long long LL;
LL a[N];
LL f[N];
int main() {
    int n;
    while (cin >> n) {
        //f[i]是以a[i]结尾的子序列并不是i取到n,f[i]就是最大的
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        LL res = a[0];
        f[0] = 0;
        for (int i = 1; i <= n; i++) {
            f[i] = max(f[i-1], (LL)0) + a[i];
            res = max(f[i], res);

        }
        cout << res << endl;
    }

}

全部评论

相关推荐

我就是0offer糕手:北大不乱杀
点赞 评论 收藏
分享
04-02 16:49
门头沟学院 Java
_bloodstream_:我也面了科大讯飞,主管面的时候听说急招人优先考虑能尽快实习的,我说忙毕设,后面就一直没消息了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务