1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public int maximumSum(int[] arr) { int n = arr.length, ans = Integer.MIN_VALUE; int[][] f = new int[n + 1][2]; Arrays.fill(f[0], Integer.MIN_VALUE / 2); for (int i = 0; i < n; i++) { f[i + 1][0] = Math.max(f[i][0], 0) + arr[i]; f[i + 1][1] = Math.max(f[i][1] + arr[i], f[i][0]); ans = Math.max(ans, Math.max(f[i + 1][0], f[i + 1][1])); } return ans; } }
|