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[[Link]];
sum[0] = A[0];
for(int i = 1; i < [Link];i++){
sum[i] = [Link](A[i],sum[i-1] + A[i]);
max = [Link](max,sum[i]);
}
return max;
}
}
-----------------------------
public int maxSubarray(int[] A){
int newsum = A[0];
int max = A[0];
for(int i=1;i<[Link];i++){
newsum = [Link](newsum+A[i],A[i]);
max = [Link](max,newsum);
}
return max;
}