Skip to content

Instantly share code, notes, and snippets.

@lysenko-sergey-developer
Created September 11, 2020 17:20
Show Gist options
  • Save lysenko-sergey-developer/1c87263f6e592880f8edc5e8b4e92a40 to your computer and use it in GitHub Desktop.
Save lysenko-sergey-developer/1c87263f6e592880f8edc5e8b4e92a40 to your computer and use it in GitHub Desktop.
function maxSubArray(nums) {
// time - O(n^3), memory - O(1)
let max = nums[0];
for (let i = 0; i <= nums.length; i++) {
for(let j = 0; j <= i; j++) {
if (i === j) continue;
const newMax = nums.slice(j, i).reduce((a, b) => a += b, 0);
max = max > newMax ? max : newMax;
}
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment