最大子序和問題:給定整數A1,A2,.....An(可能有負數),求A1 +A2+A3+......+An的最大值(規定:如果所有整數均為負數,則最大子序和為0)
例:
輸入-2, 11, -4, 13, -5, -2時,答案為20(從A2到A4)。
下面給出四種不同複雜度的演算法,進行比較分析
O(n3)的演算法(窮舉所有可能)
<span style="font-size:18px;">public static int maxSubsequenceSum(int[] nums) {int ThisSum = 0;int MaxSum = 0; for (int i = 0; i < nums.length; i++) {for (int j = i; j < nums.length; j++) {ThisSum = 0;for (int k = i; k < j; k++) { ThisSum += nums[k];} if (ThisSum > MaxSum) MaxSum = ThisSum; } } return MaxSum; }}</span>
<span style="font-size:18px;">O(n2)的演算法(去掉一次for迴圈)</span><pre name="code" class="java"><span style="font-size:18px;">public static int maxSubsequenceSum(int[] nums) {int ThisSum = 0; int MaxSum = 0;for (int i = 0; i < nums.length; i++) {ThisSum = 0; for (int j = i; j < nums.length; j++) {ThisSum += nums[j]; if (ThisSum > MaxSum) MaxSum = ThisSum; } } return MaxSum; }}</span>
O(nlogn)的演算法(利用分治的策略,代碼比較複雜)
<span style="font-size:18px;">public static int maxSubSum(int[] nums, int Left, int Right) {int MaxLeftSum, MaxRightSum;if (Left == Right){if (nums[0] > 0)return nums[0];elsereturn 0; } int Center = (Left + Right) / 2; MaxLeftSum = maxSubSum(nums, Left, Center);MaxRightSum = maxSubSum(nums, Center + 1, Right);int MaxLeftBorderSum = 0, LeftBorderSum = 0; for (int i = Center; i >= Left; i--) { LeftBorderSum += nums[i];if (LeftBorderSum > MaxLeftBorderSum) MaxLeftBorderSum = LeftBorderSum; } int MaxRightBorderSum = 0, RightBorderSum = 0;for (int i = Center + 1; i < Right; i++) {RightBorderSum += nums[i]; if (RightBorderSum > MaxRightBorderSum) MaxRightBorderSum = RightBorderSum;} int max = MaxLeftSum;max = (max < MaxLeftSum ? MaxLeftSum : max);max = (max < (MaxLeftBorderSum + MaxRightBorderSum) ? (MaxLeftBorderSum + MaxRightBorderSum) : max);return max;}</span>
O(n)的演算法(最為巧妙地演算法)
<span style="font-size:18px;">public static int maxSum(int[] nums) {int thisSum = 0;int maxSum = 0;for (int i = 0; i <nums.length; i++){thisSum += nums[i];if (maxSum < thisSum){maxSum = thisSum;}if (thisSum < 0){thisSum = 0;}}return maxSum;}</span>