Created
September 11, 2020 17:20
-
-
Save lysenko-sergey-developer/1c87263f6e592880f8edc5e8b4e92a40 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
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