maintain invariance: "max_so_far" and "max_current".
Calculate the max till current number. it only depends if the previous max is "+" or "-";
public class Solution {
public int maxSubArray(int[] nums) {
int max_current = nums[0];
int max_so_far = nums[0];
for(int i=1; i < nums.length; i++) {
max_current = max_current>0? nums[i]+max_current:nums[i];
max_so_far = Math.max(max_so_far, max_current);
}
return max_so_far;
}
}