diff --git a/lib/max_subarray.js b/lib/max_subarray.js index 46c166f..7e38948 100644 --- a/lib/max_subarray.js +++ b/lib/max_subarray.js @@ -3,7 +3,23 @@ // Space Complexity: function maxSubArray(nums) { - throw new Error("Function not implemented yet...") + + if (!nums.length) return null + + let maxSoFar = -Infinity; + let maxEndingHere = 0; + + for (num in nums){ + maxEndingHere = maxEndingHere + nums[num] + // console.log(maxEndingHere) + + if (maxSoFar < maxEndingHere) { + maxSoFar = maxEndingHere; + } else if (maxEndingHere < 0){ + maxEndingHere = 0 + } + } + return maxSoFar } module.exports = { diff --git a/lib/newman_conway.js b/lib/newman_conway.js index 42685b0..0cf1b03 100644 --- a/lib/newman_conway.js +++ b/lib/newman_conway.js @@ -3,7 +3,26 @@ // Space Complexity: function newmanConway(num) { - throw new Error("Function not implemented yet...") + + //from test, account for these base cases upfront: + if (num == 1) { + return "1"; + } else if (num == 2) { + return "1 1"; + } else if (num < 1) { + throw new Error; + } + + let conwaySequence = [1, 1]; + + for (i = 3; i <= num; i++) { + const lastValue = conwaySequence[i - 2]; + const newValue = conwaySequence[lastValue - 1] + conwaySequence[i - lastValue - 1]; + conwaySequence.push(newValue); + } + + return conwaySequence.join(' '); + } module.exports = {