Leetcode 53 : Maximum Subarray
Solution to Maximum Subarray problem. [Checkout] this resource to understand how this approach work.
class Solution {
public int maxSubArray(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int curMax = nums[i];
int allMax = nums[i];
for(int i=1;i<nums.length;i++) {
curMax = Math.max(nums[i], curMax + nums[i]);
allMax = Math.max(curMax, allMax);
}
return allmax;
}
}