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

连续子数组最大和

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = in.nextInt();
        }
        in.close();

        int[] dp = new int[n];

        dp[0] = array[0];
        int res = dp[0];
        for (int i = 1; i < n; i++) {
            dp[i] = Math.max(dp[i - 1] + array[i], array[i]);
            res = Math.max(res, dp[i]);
        }

        System.out.println(res);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
08-15 16:48
想要一个AK:问题很多加微信私聊 (一个赞十道算法题,我看看有多少)
点赞 评论 收藏
分享
我已成为0offer的糕手:你就是offer蜈蚣的蜈蚣投投,只能捡你吃剩的了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务