Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created April 13, 2016 00:53
Show Gist options
  • Save RameshRM/1235c16bc6c282df80b7dd3d2f1c53b0 to your computer and use it in GitHub Desktop.
Save RameshRM/1235c16bc6c282df80b7dd3d2f1c53b0 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
var start = 0;
var next = start + 1;
var sum1 = 0;
var sum2 = 0;
var max;
for (var i = 0; i < nums.length; i++) {
sum1 = Math.max(0, sum1 + nums[i]);
sum2 = Math.max(sum2, sum1);
if (typeof max === undefined) {
max = nums[i];
}
max = max > nums[i] ? max : nums[i];
}
return sum2 > 0 ? sum2 : max;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment