1. Maximum sum of continuous subarrays
Enter an integer array array with length n. one or consecutive integers in the array form a sub array. Find the maximum value of the sum of all subarrays.
Data range: 1 <= n <= 10^51<=n<=105-100 <= a[i] <= 100−100<=a[i]<=100
Requirement: the time complexity is O (n), space complexity is O(n)
Advanced: the time complexity is O(n), space complexity is O(1)
There are several methods:
Method 1: the violence method, taking each node of the array as the starting point, calculates the maximum value from the starting point to the end;
1 public class Solution { 2 public int FindGreatestSumOfSubArray(int[] array) { 3 int max = Integer.MIN_VALUE; 4 for(int i = 0; i < array.length; i++){ 5 int sum = lengthOfsubArray(array, i); 6 if(max < sum){ 7 max = sum; 8 } 9 } 10 return max; 11 } 12 13 public int lengthOfsubArray(int[] arr, int startNum){ 14 int max = Integer.MIN_VALUE; 15 int sum = 0; 16 for(int i = startNum; i < arr.length; i++){ 17 sum += arr[i]; 18 if(sum > max){ 19 max = sum; 20 } 21 } 22 return max; 23 } 24 }
The method may time out;
Method 2: using the method of dynamic programming to create dp[] array;
dp[i] = Math.max(dp[i-1]+array[i], array[i]);
1 public class Solution { 2 public int FindGreatestSumOfSubArray(int[] array) { 3 int length = array.length; 4 int[] dp = new int[length]; 5 int max = array[0]; 6 dp[0] = array[0]; 7 for(int i = 1; i < length; i++){ 8 if(dp[i-1] + array[i] > array[i]){ 9 dp[i] = dp[i-1]+array[i]; 10 }else{ 11 dp[i]=array[i]; 12 } 13 if(max<dp[i]){ 14 max=dp[i]; 15 } 16 } 17 return max; 18 } 19 }
2. Maximum sum of continuous subarrays (II)
Enter an integer array array with length n. one or more consecutive integers in the array form a sub array to find a continuous sub array with maximum sum. 1. The subarray is continuous. For example, the subarray of [1,3,5,7,9] has [1,3], [3,5,7], etc., but [1,3,7] is not a subarray 2. If there are multiple continuous subarrays with the largest sum, the longest one will be returned, and the data of the question will ensure that there is only one longest one 3. The minimum length of the subarray defined in this question is 1. There is no empty subarray, that is, there is no subarray where [] is an array 4. The returned array is not included in the space complexity calculation Data range: 1<=n<=10^51<=n<=105-100 <= a[i] <= 100−100<=a[i]<=100
Requirements: time complexity O(n), space complexity O(n)
Advanced: time complexity O(n), space complexity O(1)
There are the following methods; Method 1: the violence method was used to obtain the maximum sum of continuous sub arrays twice;
import java.util.*; public class Solution { /** * The class name, method name and parameter name in the code have been specified. Do not modify them. Just return the value specified by the method directly * * * @param array int Integer one-dimensional array * @return int Integer one-dimensional array */ public int[] FindGreatestSumOfSubArray (int[] array) { int max=Integer.MIN_VALUE; int sum=0; int startNum=0; int endNum=0; int length=0; for(int i=0;i<array.length;i++){ sum=0; for(int j=i;j<array.length;j++){ sum=sum+array[j]; if(sum>max){ max=sum; startNum=i; endNum=j; length=endNum-startNum+1; }else if(sum == max && length<j-i+1){ max=sum; startNum=i; endNum=j; length=endNum-startNum+1; } } } int[] result=new int[endNum-startNum+1]; for(int i=0;i<endNum-startNum+1;i++){ result[i]=array[startNum+i]; } return result; } }Method 2: the method is the same as the above problem, using the algorithm of dynamic programming;