给定一个长度为 n 的数组 arr ,返回其中任意连续子数组的最大累加和
题目保证没有全为负数的数据
数据范围:,数组中元素值
要求:时间复杂度为,空间复杂度为
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here int max=0; int sum=0; for(int pos=0;pos<arr.length;pos++){ sum+=arr[pos]; max=Math.max(max,sum); if(sum<0){ sum=0; continue; } } return max; } }
public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here int m = arr.length; int[] dp = new int[m]; Arrays.fill(dp, Integer.MIN_VALUE); int maxSum = Integer.MIN_VALUE; for(int i=0;i<m;i++){ dp[i] = getDp(arr, i, dp); maxSum = Math.max(maxSum, dp[i]); } return maxSum; } public int getDp(int[] arr, int n, int[] dp){ if(n == 0) return arr[0]; if(dp[n]!=Integer.MIN_VALUE) return dp[n]; return Math.max(arr[n], dp[n-1]+arr[n]); } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here int res=0; int sum=0; for(int i=0;i<arr.length;i++){ sum=sum+arr[i]; if(sum>res){ res=sum; } if(arr[i]<0&&sum<0){ sum=0; continue; } } return res; } }
public class Solution { public int maxsumofSubarray (int[] arr) { int max = 0; int cumsum = 0; for(int i = 0; i < arr.length; i++){ if(cumsum >= 0){ cumsum += arr[i]; } else{ cumsum = arr[i]; } max = Math.max(max, cumsum); } return max; } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here int cur = 0; int max = Integer.MIN_VALUE; for(int i = 0; i < arr.length; i++){ cur += arr[i]; max = Math.max(cur,max); cur = cur < 0 ? 0 : cur; } return max; } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here if (arr.length == 0) return 0; int[] dp = new int[arr.length]; dp[0] = arr[0]; int resMax = dp[0]; for (int i=1; i < arr.length;i++) { if (dp[i-1] > 0) dp[i] = arr[i] + dp[i-1]; else dp[i] = arr[i]; resMax = Math.max(resMax, dp[i]); } return resMax; } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here if(arr==null||arr.length==0) return 0; int max = arr[0]; int count = 0; for(int i = 0;i<arr.length;i++){ count+=arr[i]; max = Math.max(count,max); max = Math.max(max,arr[i]); if(max<=0){ count=0; } } return max; } }
//有点蠢的办法哈哈 public int maxsumofSubarray (int[] arr) { // write code here if(arr.length<=0) return 0; int left=0,right=arr.length-1; int max=Arrays.stream(arr).sum(); ArrayList<Integer> list =new ArrayList<>(); list.add(max); while(left<=right){ if(arr[left]<=arr[right]){ max=max-arr[left]; list.add(max); left++; }else{ max=max-arr[right]; list.add(max); right--; } } return Collections.max(list); } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { int max = arr[0], curMax = arr[0]; for(int i = 1; i < arr.length; i ++){ curMax = Math.max(arr[i], arr[i] + curMax); max = Math.max(max, curMax); } return max; } public int maxsumofSubarray2 (int[] arr) { //dp[i] 为以arr[i]为结尾的子数组的最大累加和 //dp[i] = max(arr[i] + dp[i-1], arr[i]) int[] dp = new int[arr.length]; dp[0] = arr[0]; int max = dp[0]; for(int i = 1; i < arr.length; i ++){ dp[i] = Math.max(arr[i], arr[i] + dp[i-1]); max = Math.max(max, dp[i]); } return max; } }
public int maxsumofSubarray (int[] arr) { int maxSum = 0; int sum = 0; for(int i = 0; i < arr.length; i++){ sum = sum + arr[i] > 0 ? sum + arr[i] : 0; maxSum = maxSum > sum ? maxSum : sum; } return maxSum; }
/** * 对于此类问题,首先要理清思路,最大子段和怎么求, * 第一步,需要用一个循环,遍历以每个数组元素开头的子段, * 其次,需要记录以i元素开头的子段和以作比较 * 接着,因为已保证没有全是负数的数组,所以可以想到要除去累加和为负的子段,从新开始 * 最后比较得到最大子段和sum */ int sum = arr[0], temp = arr[0]; for (int j = 1; j < arr.length; j++) { if (temp > 0) { temp += arr[j]; } else { temp = arr[j]; } if (sum < temp) { sum = temp; } }
import java.util.*; public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { if(arr == null || arr.length == 0){ return 0; } int res = Integer.MIN_VALUE; int sum = 0; for(int i=0;i<arr.length;++i){ sum += arr[i]; if(sum <= 0){ sum = 0; }else{ res = Math.max(res,sum); } } return res; } }