Created
April 13, 2016 00:53
-
-
Save RameshRM/1235c16bc6c282df80b7dd3d2f1c53b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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