class Solution {
public int maxSubArray(int[] nums) {
int ans = nums[0];
int n = nums.length;
int currentSum = nums[0];
for(int i = 1; i < n; i++) {
if(currentSum + nums[i] < nums[i]) {
currentSum = nums[i];
} else {
currentSum += nums[i];
}
ans = Math.max(ans, currentSum);
}
return ans;
}
}
class Solution {
public int maxSubArray(int[] nums) {
int ans = Integer.MIN_VALUE;
int sum = 0;
int n = nums.length;
for(int i = 0; i < n; i++) {
sum += nums[i];
ans = Math.max(ans, sum);
if(sum <= 0) sum = 0;
}
return ans;
}
}