Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Richelle - dynamic programming assignment #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/max_subarray.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n)
// Space Complexity: O(1) constant

function maxSubArray(nums) {
Comment on lines +2 to 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work Richelle!

throw new Error("Function not implemented yet...")
let maxSoFar = Number.NEGATIVE_INFINITY;
// or should I init maxSoFar as nums[0] but that would assume the array is not empty
// would need to check if nums is empty & return here in that case
let maxEndingHere = 0;

nums.forEach(num => { //O(n)
maxEndingHere = maxEndingHere + num;

if(maxEndingHere > maxSoFar) {
maxSoFar = maxEndingHere;
}

if(maxEndingHere < 0) {
// reset maxEndingHere, so that it doesn't accumulate
// negative nums, any single negative num would be greater
maxEndingHere = 0
}
})

if (maxSoFar == Number.NEGATIVE_INFINITY) return null;
return maxSoFar;
}

module.exports = {
Expand Down
24 changes: 21 additions & 3 deletions lib/newman_conway.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n) where n = num
// Space Complexity: O(n) - technically my in 2n because of the .slice

// P(1) = 1
// P(2) = 1
// for all n > 2
// P(n) = P(P(n - 1)) + P(n - P(n - 1))

function newmanConway(num) {
Comment on lines +2 to 10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("Function not implemented yet...")
if (num < 1) throw new Error;

let solutions = [0, 1];

if (num > 1) {
solutions.push(1);

for (let i = 3; i <= num; i++) {
solutions.push(
solutions[solutions[i -1]] + solutions[i - solutions[i -1]])
}
}

return solutions.slice(1).join(" ");
}

module.exports = {
Expand Down
Loading