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

avc278 - 3.01 - JavaScript #76

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

Conversation

avc278
Copy link
Contributor

@avc278 avc278 commented Oct 11, 2020

In addition to the solution to 3.01, I have also added stacksAndQueues.js to re-use stack and queue functionality for future problems in the chapter.

Comment on lines +23 to +28
this.stackOneStartIdx = 0;
this.stackOneCurrIdx = 0;
this.stackTwoStartIdx = arrLength / 3;
this.stackTwoCurrIdx = arrLength / 3;
this.stackThreeStartIdx = 2 * arrLength / 3;
this.stackThreeCurrIdx = 2 * arrLength / 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

if instead of implementing three stacks you had to implement four, what would need to be changed?

(spoiler alert: next question is going to be "how about five?")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

D: oh boy.

Alright, so before I go into coding, I'm going to write down a few notes just to get my brain going:
so, if our array can store N stacks, then on instantiation of the array, we need to tell it some more stuff.
And its class properties would be slightly different. perhaps three arrays:

  1. the regular array this.arr
  2. an array storing starting indices this.stackStartIdx
  3. an array storying curr indices this.stackCurrIdx
    and this way, we can accommodate for the case where we want to store N stacks of different lengths :)

* `push()` on the array does not allocate additional memory.
*
* Say we have three stacks of different lengths; we should first start off by storing the length of the longest stack,
* and creating a pseudo array whose length is 3 times that length, which we'll refer to as N. Then, at indices 0, N/3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* and creating a pseudo array whose length is 3 times that length, which we'll refer to as N. Then, at indices 0, N/3,
* and creating an array whose length is 3 times that length, which we'll refer to as N. Then, at indices 0, N/3,

the array that is being created is pretty real. nothing pseudo about it.

also, what if instead of asking for the total array length, the maximum length of each stack is passed as parameter? would that simplify stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah definitely changing the signature seems to be the way to go here. I'll tinker around and see what I can come up with

let poppedVal;
switch (stackNum) {
case 1:
if (this.arr[this.stackOneCurrIdx] === null) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

what if i really wanted to store a null?

*
*/
const threeInOne = (A, B, C) => {
const [ invA, lenA ] = invertStackAndGetLength(A);
Copy link
Contributor

Choose a reason for hiding this comment

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

normally, queue and stack implementations have an O(1) .length()/.size() operation (e.g. https://en.cppreference.com/w/cpp/container/stack/size). and an array-to-stack-then-inverted is essentially a queue, so some of the complexity needed to set this up can be avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh interesting!
I'll have to add the size() method to the library file in a future commit.
I'm wondering how I would solve this problem without the use of the queue class. Only using the given stacks, and the arrays.... Maybe as I'm pushing elements from the stacks into the arrays, I do so from the end to the start! That might work and resolve the issue of the unnecessary added complexity. I'll try that and send it in a future commit

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

Successfully merging this pull request may close these issues.

2 participants