Skip to content

Commit b131c4d

Browse files
committed
Added Validate Stack Sequences to Stack
1 parent 54905cf commit b131c4d

File tree

2 files changed

+234
-2
lines changed

2 files changed

+234
-2
lines changed

.ipynb_checkpoints/Stack-checkpoint.ipynb

+117-1
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,126 @@
417417
"};"
418418
]
419419
},
420+
{
421+
"cell_type": "markdown",
422+
"id": "d238789d",
423+
"metadata": {},
424+
"source": [
425+
"## Validate Stack Sequences\n",
426+
"\n",
427+
"* https://leetcode.com/problems/validate-stack-sequences/description/\n",
428+
"***\n",
429+
"* Time Complexity: O(n)\n",
430+
" - pushed and popped are the same length since they're just permutations of each other\n",
431+
" - you will traverse all of pushed and all of popped so it's O(2n) = O(n)\n",
432+
"* Space Complexity: O(n) with stack, O(1) using pushed as stack\n",
433+
" - you can push/pop from a stack to find the answer\n",
434+
" - using pushed as a stack will just overwrite its earlier values\n",
435+
"***\n",
436+
"* we iterate through pushed and push the values into a stack\n",
437+
"* we also keep a pointer in popped as well\n",
438+
"* if we get to a pushed value that was also popped, so if pushed[i] === popped[j]\n",
439+
" - we will not push that value but instead see if there are others in the stack that can be popped after j\n",
440+
" - so we enter a while loop for this where we continually pop from the stack and increment j\n",
441+
"* for the space-optimized version, the concept is the same except pushed can be used as the stack\n",
442+
" - we just have another pointer to keep track of where the top of the stack is and rewrite the values with later values of pushed\n",
443+
" - then once we meet the condition of pushed === popped, instead of actually popping, we just decrement pointer that points to the top of the stack\n",
444+
" * this effectively pops b/c we overwrite those values anyway\n",
445+
" - then once we've finished traversing both, we check if i === 0.\n",
446+
" * when i = 0 that means the stack is empty"
447+
]
448+
},
449+
{
450+
"cell_type": "code",
451+
"execution_count": 1,
452+
"id": "bc11b464",
453+
"metadata": {},
454+
"outputs": [],
455+
"source": [
456+
"/**\n",
457+
" * @param {number[]} pushed\n",
458+
" * @param {number[]} popped\n",
459+
" * @return {boolean}\n",
460+
" */\n",
461+
"var validateStackSequences = function(pushed, popped) {\n",
462+
" const stack = [];\n",
463+
"\n",
464+
" let j = 0;\n",
465+
" for (let i = 0; i < pushed.length; i++) {\n",
466+
" // remember that pushed and popped are INTEGER ARRAYS, NOT STACKS\n",
467+
" // so if we traverse them from L --> R, we can see the order of pushed and popped\n",
468+
"\n",
469+
" // if we pushed and then immediately popped, then this will guarantee\n",
470+
" // the items we pushed beforehand will also be popped off\n",
471+
"\n",
472+
" // e.g. \n",
473+
" // pushed = [1, 2, 0, 3, 4]\n",
474+
" // popped = [0, 2, 1, 4, 3]\n",
475+
" // stack = [1, 2]\n",
476+
" // and once we get to i = 2 and j = 0, we see that pushed (0) === popped (0)\n",
477+
" // so then we enter our while loop\n",
478+
" // j = 1, 2 is at the top of our stack so we pop\n",
479+
" // j = 2, 1 is now at the top of our stack so we pop\n",
480+
" // j = 3, stack is empty now []\n",
481+
" if (pushed[i] === popped[j]) {\n",
482+
" j++;\n",
483+
" while((stack.length > 0) && (stack[stack.length - 1] === popped[j])) {\n",
484+
" stack.pop();\n",
485+
" j++;\n",
486+
" }\n",
487+
" }\n",
488+
" else {\n",
489+
" stack.push(pushed[i]);\n",
490+
" }\n",
491+
" }\n",
492+
"\n",
493+
" return stack.length === 0;\n",
494+
"}\n",
495+
"\n",
496+
"// space - efficient\n",
497+
"var validateStackSequences = function(pushed, popped) {\n",
498+
" let i = 0;\n",
499+
" let j = 0;\n",
500+
"\n",
501+
" for (const val of pushed) {\n",
502+
" // this overwrites pushed and treats it as a stack\n",
503+
" pushed[i] = val;\n",
504+
" i++;\n",
505+
"\n",
506+
" // essentially the same as above\n",
507+
" while (i > 0 && pushed[i - 1] === popped[j]) {\n",
508+
" // decrementing i effectively removes a value from our \"stack\"\n",
509+
" // since we overwrite it later\n",
510+
" i--;\n",
511+
" j++;\n",
512+
" }\n",
513+
" }\n",
514+
"\n",
515+
" // if we are back at i, that means we have \"popped\" everything from our \"stack\"\n",
516+
" return i === 0;\n",
517+
"}\n"
518+
]
519+
},
520+
{
521+
"cell_type": "code",
522+
"execution_count": null,
523+
"id": "f800b524",
524+
"metadata": {},
525+
"outputs": [],
526+
"source": []
527+
},
528+
{
529+
"cell_type": "code",
530+
"execution_count": null,
531+
"id": "5c279eb9",
532+
"metadata": {},
533+
"outputs": [],
534+
"source": []
535+
},
420536
{
421537
"cell_type": "code",
422538
"execution_count": null,
423-
"id": "c8a008e0",
539+
"id": "e5e99a2d",
424540
"metadata": {},
425541
"outputs": [],
426542
"source": []

Stack.ipynb

+117-1
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,126 @@
417417
"};"
418418
]
419419
},
420+
{
421+
"cell_type": "markdown",
422+
"id": "d238789d",
423+
"metadata": {},
424+
"source": [
425+
"## Validate Stack Sequences\n",
426+
"\n",
427+
"* https://leetcode.com/problems/validate-stack-sequences/description/\n",
428+
"***\n",
429+
"* Time Complexity: O(n)\n",
430+
" - pushed and popped are the same length since they're just permutations of each other\n",
431+
" - you will traverse all of pushed and all of popped so it's O(2n) = O(n)\n",
432+
"* Space Complexity: O(n) with stack, O(1) using pushed as stack\n",
433+
" - you can push/pop from a stack to find the answer\n",
434+
" - using pushed as a stack will just overwrite its earlier values\n",
435+
"***\n",
436+
"* we iterate through pushed and push the values into a stack\n",
437+
"* we also keep a pointer in popped as well\n",
438+
"* if we get to a pushed value that was also popped, so if pushed[i] === popped[j]\n",
439+
" - we will not push that value but instead see if there are others in the stack that can be popped after j\n",
440+
" - so we enter a while loop for this where we continually pop from the stack and increment j\n",
441+
"* for the space-optimized version, the concept is the same except pushed can be used as the stack\n",
442+
" - we just have another pointer to keep track of where the top of the stack is and rewrite the values with later values of pushed\n",
443+
" - then once we meet the condition of pushed === popped, instead of actually popping, we just decrement pointer that points to the top of the stack\n",
444+
" * this effectively pops b/c we overwrite those values anyway\n",
445+
" - then once we've finished traversing both, we check if i === 0.\n",
446+
" * when i = 0 that means the stack is empty"
447+
]
448+
},
449+
{
450+
"cell_type": "code",
451+
"execution_count": 1,
452+
"id": "bc11b464",
453+
"metadata": {},
454+
"outputs": [],
455+
"source": [
456+
"/**\n",
457+
" * @param {number[]} pushed\n",
458+
" * @param {number[]} popped\n",
459+
" * @return {boolean}\n",
460+
" */\n",
461+
"var validateStackSequences = function(pushed, popped) {\n",
462+
" const stack = [];\n",
463+
"\n",
464+
" let j = 0;\n",
465+
" for (let i = 0; i < pushed.length; i++) {\n",
466+
" // remember that pushed and popped are INTEGER ARRAYS, NOT STACKS\n",
467+
" // so if we traverse them from L --> R, we can see the order of pushed and popped\n",
468+
"\n",
469+
" // if we pushed and then immediately popped, then this will guarantee\n",
470+
" // the items we pushed beforehand will also be popped off\n",
471+
"\n",
472+
" // e.g. \n",
473+
" // pushed = [1, 2, 0, 3, 4]\n",
474+
" // popped = [0, 2, 1, 4, 3]\n",
475+
" // stack = [1, 2]\n",
476+
" // and once we get to i = 2 and j = 0, we see that pushed (0) === popped (0)\n",
477+
" // so then we enter our while loop\n",
478+
" // j = 1, 2 is at the top of our stack so we pop\n",
479+
" // j = 2, 1 is now at the top of our stack so we pop\n",
480+
" // j = 3, stack is empty now []\n",
481+
" if (pushed[i] === popped[j]) {\n",
482+
" j++;\n",
483+
" while((stack.length > 0) && (stack[stack.length - 1] === popped[j])) {\n",
484+
" stack.pop();\n",
485+
" j++;\n",
486+
" }\n",
487+
" }\n",
488+
" else {\n",
489+
" stack.push(pushed[i]);\n",
490+
" }\n",
491+
" }\n",
492+
"\n",
493+
" return stack.length === 0;\n",
494+
"}\n",
495+
"\n",
496+
"// space - efficient\n",
497+
"var validateStackSequences = function(pushed, popped) {\n",
498+
" let i = 0;\n",
499+
" let j = 0;\n",
500+
"\n",
501+
" for (const val of pushed) {\n",
502+
" // this overwrites pushed and treats it as a stack\n",
503+
" pushed[i] = val;\n",
504+
" i++;\n",
505+
"\n",
506+
" // essentially the same as above\n",
507+
" while (i > 0 && pushed[i - 1] === popped[j]) {\n",
508+
" // decrementing i effectively removes a value from our \"stack\"\n",
509+
" // since we overwrite it later\n",
510+
" i--;\n",
511+
" j++;\n",
512+
" }\n",
513+
" }\n",
514+
"\n",
515+
" // if we are back at i, that means we have \"popped\" everything from our \"stack\"\n",
516+
" return i === 0;\n",
517+
"}\n"
518+
]
519+
},
520+
{
521+
"cell_type": "code",
522+
"execution_count": null,
523+
"id": "f800b524",
524+
"metadata": {},
525+
"outputs": [],
526+
"source": []
527+
},
528+
{
529+
"cell_type": "code",
530+
"execution_count": null,
531+
"id": "5c279eb9",
532+
"metadata": {},
533+
"outputs": [],
534+
"source": []
535+
},
420536
{
421537
"cell_type": "code",
422538
"execution_count": null,
423-
"id": "c8a008e0",
539+
"id": "e5e99a2d",
424540
"metadata": {},
425541
"outputs": [],
426542
"source": []

0 commit comments

Comments
 (0)