Created
September 12, 2020 11:09
-
-
Save lysenko-sergey-developer/3d33962f409958a881c141273cb417eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var maxSubArray = function(nums) { | |
if (nums.length === 1) return nums[0]; | |
const reserveResult = Math.max(...nums); | |
for (let i = 0; i < nums.length; i++) { | |
if (i && (nums[i] > 0 && nums[i - 1] > 0) || (nums[i - 1] + nums[i] > 0 && nums[i] < nums[i - 1] + nums[i])) { | |
nums[i] = nums[i - 1] + nums[i]; | |
} else if (nums[i] < 0) { | |
nums[i] = 0; | |
} | |
} | |
return Math.max(...nums) || reserveResult; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment