Stack Computation

Back

Loading concept...

๐Ÿฅž Stack Computation: The Magic Pancake Counter

Ever wonder how calculators solve complex math? Or how computers decode secret messages? Letโ€™s discover the magic of Stack Computation!


๐ŸŽฏ What Youโ€™ll Master

Imagine you have a stack of pancakes. You can only add or remove pancakes from the TOP. This simple rule makes stacks incredibly powerful for solving tricky problems!

graph TD A["๐Ÿฅž Pancake Stack"] --> B["Add to TOP only"] A --> C["Remove from TOP only"] B --> D["Last In, First Out"] C --> D

1๏ธโƒฃ Min Stack: The Smart Pancake Counter

๐Ÿค” The Problem

Youโ€™re stacking pancakes and your friend keeps asking: โ€œWhatโ€™s the smallest pancake in the stack RIGHT NOW?โ€

Checking every pancake each time would be slow! Can we answer instantly?

๐Ÿ’ก The Trick: Two Stacks!

Keep a helper stack that always tracks the smallest value seen so far.

class MinStack {
  constructor() {
    this.stack = [];
    this.minStack = [];  // Helper!
  }

  push(val) {
    this.stack.push(val);
    // Track minimum at this point
    const currentMin =
      this.minStack.length === 0
        ? val
        : Math.min(val, this.minStack.at(-1));
    this.minStack.push(currentMin);
  }

  pop() {
    this.stack.pop();
    this.minStack.pop();
  }

  getMin() {
    return this.minStack.at(-1);  // Instant!
  }
}

๐ŸŽฎ Example Walkthrough

Action Stack Min Stack getMin()
push(5) [5] [5] 5
push(2) [5,2] [5,2] 2
push(7) [5,2,7] [5,2,2] 2
pop() [5,2] [5,2] 2

Magic: We always know the minimum without searching! ๐ŸŽฉ


2๏ธโƒฃ Expression Parsing: Reading Math Like a Story

๐Ÿค” The Challenge

How does a computer understand 3 + 4 * 2? It doesnโ€™t just read left-to-right like us!

๐Ÿ’ก The Secret: Operator Priority

Think of it like a VIP line:

  • ร— and รท are VIPs (go first)
  • + and โˆ’ wait their turn
graph TD A["3 + 4 * 2"] --> B["See: 3"] B --> C["See: +"] C --> D["See: 4"] D --> E["See: *"] E --> F["* is VIP! Calculate 4*2 first"] F --> G["Result: 3 + 8 = 11"]

๐ŸŽฏ Two Stacks Save the Day

function evaluate(expression) {
  const nums = [];
  const ops = [];

  const priority = {'+':1, '-':1, '*':2, '/':2};

  function applyOp() {
    const b = nums.pop();
    const a = nums.pop();
    const op = ops.pop();
    if (op === '+') nums.push(a + b);
    if (op === '-') nums.push(a - b);
    if (op === '*') nums.push(a * b);
    if (op === '/') nums.push(Math.trunc(a / b));
  }

  // Parse character by character...
  // Push numbers to nums stack
  // Handle operators based on priority

  return nums[0];
}

Key Insight: Before adding a new operator, compute any waiting VIPs first!


3๏ธโƒฃ Basic Calculator: The Bracket Master

๐Ÿค” The Puzzle

2 * (3 + 4) โ€” How do we handle brackets?

๐Ÿ’ก Brackets Are Like Pause Buttons!

When you see (:

  1. Save your current work
  2. Start fresh inside brackets

When you see ):

  1. Finish bracket calculation
  2. Resume saved work
function calculate(s) {
  const stack = [];
  let num = 0;
  let result = 0;
  let sign = 1;

  for (const char of s) {
    if (char >= '0' && char <= '9') {
      num = num * 10 + Number(char);
    } else if (char === '+') {
      result += sign * num;
      num = 0;
      sign = 1;
    } else if (char === '-') {
      result += sign * num;
      num = 0;
      sign = -1;
    } else if (char === '(') {
      // SAVE current state
      stack.push(result);
      stack.push(sign);
      result = 0;
      sign = 1;
    } else if (char === ')') {
      result += sign * num;
      num = 0;
      // RESTORE saved state
      result *= stack.pop();  // sign
      result += stack.pop();  // previous result
    }
  }

  return result + sign * num;
}

๐ŸŽฎ Example: 1 + (2 - 3)

Step Action Stack Result
1 num = 1 [] 0
+ result = 1 [] 1
( save state [1, 1] 0
2 num = 2 [1, 1] 0
- result = 2 [1, 1] 2
3 num = 3 [1, 1] 2
) finish & restore [] 0

4๏ธโƒฃ Decode String: The Secret Message Unlocker

๐Ÿค” The Mystery

3[ab2[c]] means โ€œrepeat ab2[c] three timesโ€โ€ฆ but whatโ€™s inside?

๐Ÿ’ก Work from Inside Out!

Like Russian nesting dolls ๐Ÿช† โ€” open the innermost first!

graph TD A["3[ab2[c]]"] --> B["Find innermost: 2[c]"] B --> C["Decode: cc"] C --> D["Now have: 3[abcc]"] D --> E["Decode: abccabccabcc"]

๐ŸŽฏ The Stack Solution

function decodeString(s) {
  const countStack = [];
  const stringStack = [];
  let currentStr = '';
  let k = 0;

  for (const char of s) {
    if (char >= '0' && char <= '9') {
      k = k * 10 + Number(char);
    } else if (char === '[') {
      // SAVE current progress
      countStack.push(k);
      stringStack.push(currentStr);
      currentStr = '';
      k = 0;
    } else if (char === ']') {
      // DECODE and combine
      const count = countStack.pop();
      const prevStr = stringStack.pop();
      currentStr = prevStr +
        currentStr.repeat(count);
    } else {
      currentStr += char;
    }
  }

  return currentStr;
}

๐ŸŽฎ Walkthrough: 2[a3[b]]

Char Action String Stack Count Stack Current
2 k=2 [] [] โ€œโ€
[ save [โ€œโ€] [2] โ€œโ€
a add [โ€œโ€] [2] โ€œaโ€
3 k=3 [โ€œโ€] [2] โ€œaโ€
[ save [โ€œโ€,โ€œaโ€] [2,3] โ€œโ€
b add [โ€œโ€,โ€œaโ€] [2,3] โ€œbโ€
] decode [โ€œโ€] [2] โ€œabbbโ€
] decode [] [] โ€œabbbabbbโ€

5๏ธโƒฃ Asteroid Collision: The Space Battle

๐Ÿค” The Scenario

Asteroids fly in a line. Positive = right โ†’, Negative = left โ†

When they collide:

  • Bigger one survives
  • Same size? Both explode! ๐Ÿ’ฅ

๐Ÿ’ก Think Like a Space Traffic Controller

Process asteroids left to right. Use a stack to track survivors!

function asteroidCollision(asteroids) {
  const stack = [];

  for (const ast of asteroids) {
    let destroyed = false;

    // Collision: top going right, current going left
    while (
      stack.length > 0 &&
      stack.at(-1) > 0 &&
      ast < 0
    ) {
      const top = stack.at(-1);

      if (top < Math.abs(ast)) {
        stack.pop();  // Top destroyed
      } else if (top === Math.abs(ast)) {
        stack.pop();  // Both destroyed
        destroyed = true;
        break;
      } else {
        destroyed = true;  // Current destroyed
        break;
      }
    }

    if (!destroyed) {
      stack.push(ast);
    }
  }

  return stack;
}

๐ŸŽฎ Battle Simulation: [5, 10, -5]

Asteroid Stack Before Action Stack After
5โ†’ [] Add [5]
10โ†’ [5] Add [5, 10]
โ†5 [5, 10] 10 vs 5: 10 wins [5, 10]

Result: [5, 10] โ€” The small asteroid was destroyed!


6๏ธโƒฃ Remove K Digits: Make the Smallest Number

๐Ÿค” The Challenge

Given "1432219" and k=3, remove 3 digits to make the smallest number.

๐Ÿ’ก The Greedy Stack Strategy

Scan left to right. If a digit is bigger than the next one, itโ€™s a good candidate to remove!

graph TD A["1432219, remove 3"] --> B["1 &lt; 4: keep 1"] B --> C["4 &gt; 3: remove 4"] C --> D["3 &gt; 2: remove 3"] D --> E["2 &gt; 1: remove 2"] E --> F["Result: 1219"]

๐ŸŽฏ The Stack Solution

function removeKdigits(num, k) {
  const stack = [];

  for (const digit of num) {
    // Remove larger digits from stack
    while (
      k > 0 &&
      stack.length > 0 &&
      stack.at(-1) > digit
    ) {
      stack.pop();
      k--;
    }
    stack.push(digit);
  }

  // Remove remaining from end
  while (k > 0) {
    stack.pop();
    k--;
  }

  // Remove leading zeros
  const result = stack.join('')
    .replace(/^0+/, '');

  return result || '0';
}

๐ŸŽฎ Example: "1432219", k=3

Digit Stack k Action
1 [1] 3 push
4 [1,4] 3 push
3 [1,3] 2 pop 4, push 3
2 [1,2] 1 pop 3, push 2
2 [1,2,2] 1 push
1 [1,2,1] 0 pop 2, push 1
9 [1,2,1,9] 0 push

Result: "1219" โœจ


๐ŸŽŠ You Did It!

Youโ€™ve mastered the six superpowers of Stack Computation:

Problem Key Insight
Min Stack Two stacks track everything
Expression Parsing Operators have VIP levels
Basic Calculator Brackets = Save & Resume
Decode String Innermost first, like nesting dolls
Asteroid Collision Process & battle survivors
Remove K Digits Greedily remove larger digits
graph LR A["๐Ÿฅž Stack"] --> B["Min Stack"] A --> C["Calculators"] A --> D["String Decoding"] A --> E["Simulations"] A --> F["Optimization"] B --> G["โœจ O&#35;40;1&#35;41; minimum"] C --> H["โœจ Handle any expression"] D --> I["โœจ Nested patterns"] E --> J["โœจ Collision detection"] F --> K["โœจ Greedy removal"]

Remember: Stacks are simple (Last In, First Out), but combined with clever thinking, they solve incredibly complex problems!

๐Ÿš€ Now go stack up your skills!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.