Skip to content

Instantly share code, notes, and snippets.

@lysenko-sergey-developer
Created September 12, 2020 11:09
Show Gist options
  • Save lysenko-sergey-developer/3d33962f409958a881c141273cb417eb to your computer and use it in GitHub Desktop.
Save lysenko-sergey-developer/3d33962f409958a881c141273cb417eb to your computer and use it in GitHub Desktop.
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