-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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/) |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍