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

Cedar-Bailey #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Cedar-Bailey #2

wants to merge 3 commits into from

Conversation

beaniebaye
Copy link

No description provided.

Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

✨ 💫 Looks good, Bailey! I left some comments on your implementation below.

🟢


// let i;
let array = [];
array[0] = 0;

Choose a reason for hiding this comment

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

✨ Nice use of a buffer slot to account for the 1-based calculation.


function newmanConway(num) {
throw new Error("Function not implemented yet...")
}
if (num === 0) throw new Error();

Choose a reason for hiding this comment

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

We should raise this error for any value below the valid starting point of the sequence:

  if (num < 1) throw new Error();

if (num === 1) return '1';

for (let i = 3; i <= num; i++) {
array[i] = array[array[i - 1]] + array[i - array[i - 1]];

Choose a reason for hiding this comment

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

In general, be careful assigning to arbitrary indices in an array that haven't been initializes. Yes, JS will grow the array to fill any missing intervening indices, but it can have surprising effects. Here, since we're adding each next index with no gaps, everything works as expected, but consider using push to make it more obvious that we are growing the array here.

Comment on lines +2 to +3
// Time Complexity: O(n)
// Space Complexity: O(n)

Choose a reason for hiding this comment

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

✨ Great! By carefully building up the calculations and storing them for later use, we only need to perform O(n) calculations. The storage to keep those calculations is related to n (as is the converted string) giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings).

let maxSum = nums[0];
let sum = 0;

for (let num of nums) {

Choose a reason for hiding this comment

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

👀 Perhaps surprisingly, for a for/of the loop variable can be const.

  for (const num of nums) {

Comment on lines +2 to +3
// Time Complexity: O(n) n is the size of the array
// Space Complexity: O(1)

Choose a reason for hiding this comment

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

✨ Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one).

Since like the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation, we can do it with a constant O(1) amount of storage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants