Conversation
| this.stackOneStartIdx = 0; | ||
| this.stackOneCurrIdx = 0; | ||
| this.stackTwoStartIdx = arrLength / 3; | ||
| this.stackTwoCurrIdx = arrLength / 3; | ||
| this.stackThreeStartIdx = 2 * arrLength / 3; | ||
| this.stackThreeCurrIdx = 2 * arrLength / 3; |
There was a problem hiding this comment.
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?")
There was a problem hiding this comment.
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:
- the regular array
this.arr - an array storing starting indices
this.stackStartIdx - 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, |
There was a problem hiding this comment.
| * 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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
what if i really wanted to store a null?
| * | ||
| */ | ||
| const threeInOne = (A, B, C) => { | ||
| const [ invA, lenA ] = invertStackAndGetLength(A); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
In addition to the solution to 3.01, I have also added
stacksAndQueues.jsto re-use stack and queue functionality for future problems in the chapter.