题解 | #连续子数组最大和#

连续子数组最大和

https://www.nowcoder.com/practice/1718131e719746e9a56fb29c40cc8f95

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin>>n;
    vector<int> nums(n, 0);

    for(int i = 0; i < n; i++){
        cin>>nums[i];
    }
    vector<int> dp(n, 0);
    if(n == 1){
        cout<<nums[0];
        return 0;
    }
    dp[0] = nums[0];
    int m = nums[0];
    for(int i = 1; i < n; i++){
        dp[i] = max(nums[i] + dp[i - 1], nums[i]);
        m = max(m, dp[i]);
    }
    cout<<m;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

07-29 12:14
门头沟学院 Java
程序员小白条:oppo不用想了,经常看92都挂的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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