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
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
22 changes: 19 additions & 3 deletions lib/max_subarray.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@

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

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.


function maxSubArray(nums) {
throw new Error("Function not implemented yet...")
if (nums.length === 0) return null;

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) {

sum += num;

if (sum > maxSum) {
maxSum = sum;
}
if (sum < 0) {
sum = 0;
}
}

return maxSum;
}

module.exports = {
Expand Down
24 changes: 20 additions & 4 deletions lib/newman_conway.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@

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

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).


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();


// 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.

array[1] = 1;
array[2] = 1;

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.

}

array.shift(); //O(n) operation

return array.join(' ');
};

module.exports = {
newmanConway
Expand Down
Loading