public class MaxSubArray {
public static void main(String[] args) {
int[] nums = new int[]{1,2,-1,3,4,5};
int result = getMaxSubArray(nums);
System.out.println(result);
}
/**
* [1,2,-1,3,4,5]
* @param nums
*/
public static int getMaxSubArray(int[] nums) {
int thissum = 0, maxsum = Integer.MIN_VALUE;
for(int n : nums) {
thissum = thissum + n;
if(thissum > maxsum) {
maxsum = thissum;
}
if(thissum < 0) {
thissum = 0;
}
}
return maxsum;
}
}