|
| 1 | +package ch02; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by cookfront on 2017/2/14. |
| 8 | + */ |
| 9 | +public class MaxSubSum { |
| 10 | + public static void main(String ...args) { |
| 11 | + int[] subs = {-2, 11, -4, 13, -5, -2}; |
| 12 | + System.out.println(maxSubSum1(subs)); |
| 13 | + System.out.println(maxSubSum2(subs)); |
| 14 | + System.out.println(maxSubSum4(subs)); |
| 15 | + } |
| 16 | + |
| 17 | + public static int maxSubSum1(int[] arr) { |
| 18 | + int maxSum = 0; |
| 19 | + |
| 20 | + for (int i = 0; i < arr.length; i++) { |
| 21 | + for (int j = i; j < arr.length; j++) { |
| 22 | + int thisSum = 0; |
| 23 | + |
| 24 | + for (int k = i; k <= j; k++) { |
| 25 | + thisSum += arr[k]; |
| 26 | + } |
| 27 | + |
| 28 | + if (thisSum > maxSum) { |
| 29 | + maxSum = thisSum; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return maxSum; |
| 35 | + } |
| 36 | + |
| 37 | + public static int maxSubSum2(int[] arr) { |
| 38 | + int maxSum = 0; |
| 39 | + |
| 40 | + for (int i = 0; i < arr.length; i++) { |
| 41 | + int thisSum = 0; |
| 42 | + for (int j = i; j < arr.length; j++) { |
| 43 | + thisSum += arr[j]; |
| 44 | + |
| 45 | + if (thisSum > maxSum) { |
| 46 | + maxSum = thisSum; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return maxSum; |
| 52 | + } |
| 53 | + |
| 54 | + public static int maxSumRec(int[] arr, int left, int right) { |
| 55 | + if (left == right) { |
| 56 | + if (arr[left] > 0) { |
| 57 | + return arr[left]; |
| 58 | + } else { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + int center = (left + right / 2); |
| 64 | + int maxLeftSum = maxSumRec(arr, left, center); |
| 65 | + int maxRightSum = maxSumRec(arr, center + 1, right); |
| 66 | + |
| 67 | + int maxLeftBorderSum = 0; |
| 68 | + int leftBorderSum = 0; |
| 69 | + for (int i = center; i >= left; i--) { |
| 70 | + leftBorderSum += arr[i]; |
| 71 | + if (leftBorderSum > maxLeftBorderSum) { |
| 72 | + maxLeftBorderSum = leftBorderSum; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + int maxRightBorderSum = 0; |
| 77 | + int rightBorderSum = 0; |
| 78 | + for (int i = center + 1; i <= right; i++) { |
| 79 | + rightBorderSum += arr[i]; |
| 80 | + if (rightBorderSum > maxRightBorderSum) { |
| 81 | + maxRightBorderSum = rightBorderSum; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum); |
| 86 | + } |
| 87 | + |
| 88 | + public static int max3(int a, int b, int c) { |
| 89 | + if (a > b && a > c) { |
| 90 | + return a; |
| 91 | + } else if (b > a && b > c) { |
| 92 | + return b; |
| 93 | + } else if (c > a && c > b) { |
| 94 | + return c; |
| 95 | + } |
| 96 | + return Integer.MIN_VALUE; |
| 97 | + } |
| 98 | + |
| 99 | + public static int maxSubSum3(int[] arr) { |
| 100 | + return maxSumRec(arr, 0, arr.length - 1); |
| 101 | + } |
| 102 | + |
| 103 | + public static int maxSubSum4(int[] arr) { |
| 104 | + int maxSum = 0; |
| 105 | + int thisSum = 0; |
| 106 | + |
| 107 | + for (int i = 0; i < arr.length; i++) { |
| 108 | + thisSum += arr[i]; |
| 109 | + |
| 110 | + if (thisSum > maxSum) { |
| 111 | + maxSum = thisSum; |
| 112 | + } else if (thisSum < 0) { |
| 113 | + thisSum = 0; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return maxSum; |
| 118 | + } |
| 119 | +} |
0 commit comments