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

Aimee 👾 - Fire 🔥 #2

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
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
# Dynamic Programming
Based on the Ruby version from [AdaGold/dynamic-programming](https://github.com/Ada-C12/dynamic-programming).

## Running
To run this repo, clone and install it, then run tests with `npm run test`.

# Dynamic Programming

Dynamic programming is a strategy for developing an algorithm where each subproblem is solved and the results recorded for use in solving larger problems. In this exercise you will write a pair of dynamic programming methods.

## Wave 1 Newman-Conway Sequence

[Newman-Conway sequence] is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7….. and follows below recursive formula.

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

Given a number n then print n terms of Newman-Conway Sequence

Examples:

```
Input : 13
Output : 1 1 2 2 3 4 4 4 5 6 7 7 8

Input : 20
Output : 1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
```

You should be able to do this in O(n) time complexity.

## Wave 2 Largest Sum Contiguous Subarray

Write a method to find the contiguous subarray in a 1-dimensional array with the largest sum.

![Largest subarray](images/kadane-Algorithm.png)

This can be solved using Kadane's Algorithm

```
Initialize:
max_so_far = 0
max_ending_here = 0

Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
return max_so_far
```

### Explanation

The idea of the Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of the maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far

There is also a subtle divide & conquer algorithm for this.

## Sources

- [GeeksforGeeks: Newman-Conway Sequence](https://www.geeksforgeeks.org/newman-conway-sequence/)
- [GeeksforGeeks: Largest Sum Contiguous Subarray](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/)
25 changes: 22 additions & 3 deletions lib/max_subarray.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@

// Time Complexity:
// Space Complexity:
// Time Complexity: O(n) - we iterate n times through nums, which is an array of length n
// Space Complexity: O(1) - we have constant space complexity, with variables max_result and temp_max

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.

👍

throw new Error("Function not implemented yet...")
if (nums.length < 1) {
return null;
}

let max_result = -Infinity,
temp_max = 0;

for (let i = 0; i < nums.length; i++) {
temp_max += nums[i]

if (max_result < temp_max) {
max_result = temp_max;
}

if (temp_max < 0) {
temp_max = 0;
}
}

return max_result;
}

module.exports = {
Expand Down
36 changes: 32 additions & 4 deletions lib/newman_conway.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
// Newman-Conway sequence: P(n) = P( P(n - 1)) + P(n - P(n - 1)), with intial conditions P(1)=1 and P(2)=1
// Time Complexity: O(n) - there is a for loop in newmanConway that will run n number of times. The function p that it calls on has a time complexity of O(1) because it directly looks up the values in the array it's adding to.
// Space Complexity: O(n) - both the newmanConway and p functions add n elements to their data structures.

// Time Complexity:
// Space Complexity:
newmanConway = (num) => {
Comment on lines +1 to +5

Choose a reason for hiding this comment

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

⚠️ , this works, but because you're not using your memo pValues, your recursion is making this O(3^n) .

Instead you should loop from 1 to num calculating the newmanConway value and storing it in your "memo." for lookup later.

let result = "1 1 "

function newmanConway(num) {
throw new Error("Function not implemented yet...")
if (num < 1) {
throw Error("Number must be greater than or equal to 1");
} else if (num === 1) {
return result[0];

} else if (num === 2) {
return result.trim();
}

for (let n = 3; n <= num; n++) {
result += `${p(n)} `;
}
return result.trim();
}

const pValues = [0, 1, 1]

p = (n) => {
if (pValues[n]) {
return pValues[n];
}

const pOfN = p(p(n - 1)) + p(n - p(n - 1));
pValues.push(pOfN);
return pOfN;
}



module.exports = {
newmanConway
};
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.