题目和力扣53题相同,动态规划解决
import java.util.*;
public class Main{
public static int maxSubArray(int[] nums) {
int res = nums[0];
for(int i = 1; i < nums.length; i++) {
nums[i] += Math.max(nums[i - 1], 0);
res = Math.max(res, nums[i]);
}
return res;
}
public static void main (String[] args) {
Scanner scanner=new Scanner (System.in);
int a[]=new int[1000010];
int k=scanner.nextInt ();
for (int i = 0 ; i <k ; i++) {
a[i]=scanner.nextInt ();
}
System.out.println (maxSubArray (a));
}
}