Maximum Subarray
Find the contiguous subarray within an array(containing at least on number) which has the largest sum.
Given the array [-2,1,-3,4,-1,2,1,-5,4] the contiguous subaray[4,-1,2,1] has the largest sum = 6
public class Solution{
public int maxSubarray(int[] A){
int max = A[0];
int[] sum = new int[A.length];
sum[0] = A[0];
for(int i = 1; i < A.length;i++){
sum[i] = Math.max(A[i],sum[i-1] + A[i]);
max = Math.max(max,sum[i]);
}
return max;
}
}
-----------------------------
public int maxSubarray(int[] A){
int newsum = A[0];
int max = A[0];
for(int i=1;i<A.length;i++){
newsum = Math.max(newsum+A[i],A[i]);
max = Math.max(max,newsum);
}
return max;
}