O(N^2)
package heng.java.level1; import java.util.Scanner; public class TheMostLongSequenceSum4 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int m = input.nextInt(); while(m-->0){ int n = input.nextInt(); int [] arr = new int [n]; for (int i = 0; i < n; i++) { arr[i] = input.nextInt(); } int max = maxSubSum(arr); System.out.println(max); } } public static int maxSubSum(int []arr){ int maxSum = 0; for (int i = 0; i < arr.length; i++) { int thisSum = 0; for (int j = i; j < arr.length; j++) { thisSum += arr[i]; if(thisSum > maxSum){ maxSum = thisSum; } } } return maxSum; } }
O(n) 动态规划
package heng.java.level1; import java.util.Scanner; public class TheMostLongSequenceSum3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int m = input.nextInt(); while(m-->0){ int n = input.nextInt(); int [] arr = new int [n]; for (int i = 0; i < n; i++) { arr[i] = input.nextInt(); } int max = maxSubSum(arr); System.out.println(max); } } public static int maxSubSum(int []arr){ int maxSum = 0, thisSum = 0; for(int j=0; j<arr.length; j++){ thisSum += arr[j]; if(thisSum > maxSum){ maxSum = thisSum; }else if(thisSum < 0){ thisSum = 0; } } return maxSum; } }
O(N)
递归&&分治法:
例子:4 ,-3,5,-2,-1,2,6,-2
第一次分治:左={4 ,-3,5,-2}下标:0~3------右={-1,2,6,-2}下标:4~7
左边第二次分治:左={4 ,-3}下标:0~1-------右={5,-2}下标:2~3
左边第三次分治:左={4}下标:0~0 ----- 右={-3}下标:1~1
第三次分治递归左:返回4,右返回-3,经过两个for循环,比较4,-3,(4+-3)=1这三个数的大小,取最大的,得maxSum=4 (一)
返回上一层(第二次分治时):右={5,-2}下标:2~3 同理如(一)得:右 maxSum=5
返回上一层(第一次分治时):左:{4}右:{5}同理如(一)得:maxSum=6(比较4,5,((4+-3)+(5+-2)))这个三个的大小,最大的,得6
(以上为第一次分治时的左半部份)接下来同理,得右半部份为8
package heng.java.level1; import java.util.Scanner; public class TheMostLongSequenceSum2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int m = input.nextInt(); while(m-->0){ int n = input.nextInt(); int [] arr = new int [n]; for (int i = 0; i < n; i++) { arr[i] = input.nextInt(); } int max = maxSumRec(arr,0,arr.length-1); System.out.println(max); } } public static int maxSumRec(int []arr, int left, int right){ if(left == right){ if(arr[left] > 0){ return arr[left]; }else{ return 0; } } int center = (left+right)/2; int maxLeftSum = maxSumRec(arr,left,center); int maxRightSum = maxSumRec(arr,center+1,right); int maxLeftBorderSum=0,leftBorderSum=0; for(int i=center; i>=left; i--){ leftBorderSum += arr[i]; if(leftBorderSum > maxLeftBorderSum){ maxLeftBorderSum = leftBorderSum; } } int maxRightBorderSum=0,rightBorderSum=0; for(int i=center+1; i<=right; i++){ rightBorderSum += arr[i]; if(rightBorderSum > maxRightBorderSum){ maxRightBorderSum = rightBorderSum; } } int sum = maxRightBorderSum+maxLeftBorderSum; if(sum < maxLeftSum) sum = maxLeftSum; if(sum < maxRightSum) sum = maxRightSum; return sum; } }
同动态规划:
package heng.java.level1; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TheMostLongSequenceSum { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); Scanner input = new Scanner(System.in); int m = input.nextInt(); while(m-->0){ int n = input.nextInt(); for (int i = 0; i < n; i++) { list.add(input.nextInt()); } int max = 0; int []arr = new int [list.size()]; arr[0] = list.get(0); for (int j = 1; j < list.size(); j++) { if(arr[j-1] <= 0){ arr[j]=list.get(j); } else{ arr[j] = arr[j-1]+list.get(j); } } for (int i = 0; i < list.size(); i++) { if(max < arr[i]){ max = arr[i]; } } System.out.println(max); } } }