题解 | #连续子数组最大和#
连续子数组最大和
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); } }