1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public int maxSubArray(int[] nums) { int n = nums.length; if (n == 0) { return 0; } int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] = nums[i - 1]; }
for (int i = 1; i <= n; i++) { dp[i] = Math.max(dp[i], dp[i - 1] + dp[i]); } int res = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { res = Math.max(res, dp[i]); } return res; } }
|