Episode 7 — DSA with JavaScript / 7.14 — Stack

7.14.a — Stack Implementation & Algorithms

<< 7.14 Overview


What is a stack?

A stack is a LIFO (Last-In, First-Out) data structure.

       ┌───┐
Push → │ 4 │ ← Pop/Peek
       ├───┤
       │ 3 │
       ├───┤
       │ 2 │
       ├───┤
       │ 1 │
       └───┘

JavaScript

class Stack {
    #items = [];
    push(val) { this.#items.push(val); }
    pop() {
        if (this.isEmpty()) throw new Error("Stack underflow");
        return this.#items.pop();
    }
    peek() { return this.#items[this.#items.length - 1]; }
    isEmpty() { return this.#items.length === 0; }
    size() { return this.#items.length; }
}

C++

#include <stack>
stack<int> s;
s.push(1);
s.top();
s.pop();
s.empty();
s.size();

Stack algorithms

Valid parentheses

function isValid(s) {
    const stack = [];
    const map = { ')': '(', ']': '[', '}': '{' };
    for (const c of s) {
        if ('([{'.includes(c)) stack.push(c);
        else if (stack.pop() !== map[c]) return false;
    }
    return stack.length === 0;
}

Min Stack

class MinStack {
    #stack = [];
    #minStack = [];
    push(val) {
        this.#stack.push(val);
        const min = this.#minStack.length ? Math.min(val, this.getMin()) : val;
        this.#minStack.push(min);
    }
    pop() { this.#stack.pop(); this.#minStack.pop(); }
    top() { return this.#stack[this.#stack.length - 1]; }
    getMin() { return this.#minStack[this.#minStack.length - 1]; }
}

Next Greater Element

Array: [4, 5, 2, 25]
Stack approach (right to left):

i=3: stack empty → NGE[3]=-1, push 25. Stack: [25]
i=2: 2 < 25 → NGE[2]=25, push 2. Stack: [25, 2]
i=1: 5 > 2 → pop. 5 < 25 → NGE[1]=25, push 5. Stack: [25, 5]
i=0: 4 < 5 → NGE[0]=5, push 4. Stack: [25, 5, 4]

Result: [5, 25, 25, -1]
function nextGreater(arr) {
    const result = new Array(arr.length).fill(-1);
    const stack = [];
    for (let i = arr.length - 1; i >= 0; i--) {
        while (stack.length && stack[stack.length - 1] <= arr[i]) stack.pop();
        if (stack.length) result[i] = stack[stack.length - 1];
        stack.push(arr[i]);
    }
    return result;
}

Evaluate postfix

function evalPostfix(tokens) {
    const stack = [];
    for (const t of tokens) {
        if ("+-*/".includes(t)) {
            const b = stack.pop(), a = stack.pop();
            switch (t) {
                case '+': stack.push(a + b); break;
                case '-': stack.push(a - b); break;
                case '*': stack.push(a * b); break;
                case '/': stack.push(Math.trunc(a / b)); break;
            }
        } else {
            stack.push(Number(t));
        }
    }
    return stack[0];
}

Largest rectangle in histogram

function largestRectangle(heights) {
    const stack = [];
    let maxArea = 0;
    const h = [...heights, 0];
    for (let i = 0; i < h.length; i++) {
        while (stack.length && h[stack[stack.length - 1]] > h[i]) {
            const height = h[stack.pop()];
            const width = stack.length ? i - stack[stack.length - 1] - 1 : i;
            maxArea = Math.max(maxArea, height * width);
        }
        stack.push(i);
    }
    return maxArea;
}

Applications

  1. Function call stack (recursion)
  2. Undo/redo functionality
  3. Expression evaluation (postfix, infix)
  4. Backtracking algorithms
  5. Browser history (back/forward)
  6. Syntax parsing (balanced brackets)

Scenario bank (stack)

7.14a-001 — Sort a stack using recursion (v1)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-002 — Reverse a string using stack (v2)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-003 — Stock span problem (v3)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-004 — Celebrity problem (v4)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-005 — Implement queue using stacks (v5)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-006 — Trapping rain water using stack (v6)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-007 — Daily temperatures (next warmer) (v7)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-008 — Remove K digits for smallest number (v8)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-009 — Decode string with nested brackets (v9)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-010 — Asteroid collision simulation (v10)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-011 — Basic calculator (with +, -, (, )) (v11)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-012 — Remove duplicate letters (v12)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-013 — 132 pattern detection (v13)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-014 — Maximal rectangle (histogram extension) (v14)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-015 — Score of parentheses (v15)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-016 — Validate stack sequences (v16)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-017 — Minimum add to make parentheses valid (v17)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-018 — Exclusive time of functions (v18)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-019 — Car fleet problem (v19)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-020 — Simplify Unix path (v20)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-021 — Sort a stack using recursion (v21)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-022 — Reverse a string using stack (v22)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-023 — Stock span problem (v23)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-024 — Celebrity problem (v24)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-025 — Implement queue using stacks (v25)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-026 — Trapping rain water using stack (v26)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-027 — Daily temperatures (next warmer) (v27)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-028 — Remove K digits for smallest number (v28)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-029 — Decode string with nested brackets (v29)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-030 — Asteroid collision simulation (v30)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-031 — Basic calculator (with +, -, (, )) (v31)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-032 — Remove duplicate letters (v32)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-033 — 132 pattern detection (v33)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-034 — Maximal rectangle (histogram extension) (v34)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-035 — Score of parentheses (v35)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-036 — Validate stack sequences (v36)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-037 — Minimum add to make parentheses valid (v37)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-038 — Exclusive time of functions (v38)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-039 — Car fleet problem (v39)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-040 — Simplify Unix path (v40)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-041 — Sort a stack using recursion (v41)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-042 — Reverse a string using stack (v42)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-043 — Stock span problem (v43)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-044 — Celebrity problem (v44)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-045 — Implement queue using stacks (v45)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-046 — Trapping rain water using stack (v46)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-047 — Daily temperatures (next warmer) (v47)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-048 — Remove K digits for smallest number (v48)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-049 — Decode string with nested brackets (v49)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-050 — Asteroid collision simulation (v50)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-051 — Basic calculator (with +, -, (, )) (v51)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-052 — Remove duplicate letters (v52)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-053 — 132 pattern detection (v53)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-054 — Maximal rectangle (histogram extension) (v54)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-055 — Score of parentheses (v55)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-056 — Validate stack sequences (v56)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-057 — Minimum add to make parentheses valid (v57)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-058 — Exclusive time of functions (v58)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-059 — Car fleet problem (v59)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-060 — Simplify Unix path (v60)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-061 — Sort a stack using recursion (v61)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-062 — Reverse a string using stack (v62)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-063 — Stock span problem (v63)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-064 — Celebrity problem (v64)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-065 — Implement queue using stacks (v65)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-066 — Trapping rain water using stack (v66)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-067 — Daily temperatures (next warmer) (v67)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-068 — Remove K digits for smallest number (v68)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-069 — Decode string with nested brackets (v69)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-070 — Asteroid collision simulation (v70)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-071 — Basic calculator (with +, -, (, )) (v71)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-072 — Remove duplicate letters (v72)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-073 — 132 pattern detection (v73)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-074 — Maximal rectangle (histogram extension) (v74)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-075 — Score of parentheses (v75)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-076 — Validate stack sequences (v76)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-077 — Minimum add to make parentheses valid (v77)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-078 — Exclusive time of functions (v78)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-079 — Car fleet problem (v79)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-080 — Simplify Unix path (v80)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-081 — Sort a stack using recursion (v81)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-082 — Reverse a string using stack (v82)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-083 — Stock span problem (v83)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-084 — Celebrity problem (v84)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-085 — Implement queue using stacks (v85)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-086 — Trapping rain water using stack (v86)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-087 — Daily temperatures (next warmer) (v87)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-088 — Remove K digits for smallest number (v88)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-089 — Decode string with nested brackets (v89)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-090 — Asteroid collision simulation (v90)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-091 — Basic calculator (with +, -, (, )) (v91)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-092 — Remove duplicate letters (v92)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-093 — 132 pattern detection (v93)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-094 — Maximal rectangle (histogram extension) (v94)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-095 — Score of parentheses (v95)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-096 — Validate stack sequences (v96)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-097 — Minimum add to make parentheses valid (v97)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-098 — Exclusive time of functions (v98)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-099 — Car fleet problem (v99)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-100 — Simplify Unix path (v100)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-101 — Sort a stack using recursion (v101)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-102 — Reverse a string using stack (v102)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-103 — Stock span problem (v103)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-104 — Celebrity problem (v104)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-105 — Implement queue using stacks (v105)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-106 — Trapping rain water using stack (v106)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-107 — Daily temperatures (next warmer) (v107)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-108 — Remove K digits for smallest number (v108)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-109 — Decode string with nested brackets (v109)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-110 — Asteroid collision simulation (v110)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-111 — Basic calculator (with +, -, (, )) (v111)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-112 — Remove duplicate letters (v112)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-113 — 132 pattern detection (v113)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-114 — Maximal rectangle (histogram extension) (v114)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-115 — Score of parentheses (v115)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-116 — Validate stack sequences (v116)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-117 — Minimum add to make parentheses valid (v117)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-118 — Exclusive time of functions (v118)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-119 — Car fleet problem (v119)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-120 — Simplify Unix path (v120)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-121 — Sort a stack using recursion (v121)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-122 — Reverse a string using stack (v122)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-123 — Stock span problem (v123)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-124 — Celebrity problem (v124)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-125 — Implement queue using stacks (v125)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-126 — Trapping rain water using stack (v126)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-127 — Daily temperatures (next warmer) (v127)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-128 — Remove K digits for smallest number (v128)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-129 — Decode string with nested brackets (v129)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-130 — Asteroid collision simulation (v130)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-131 — Basic calculator (with +, -, (, )) (v131)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-132 — Remove duplicate letters (v132)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-133 — 132 pattern detection (v133)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-134 — Maximal rectangle (histogram extension) (v134)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-135 — Score of parentheses (v135)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-136 — Validate stack sequences (v136)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-137 — Minimum add to make parentheses valid (v137)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-138 — Exclusive time of functions (v138)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-139 — Car fleet problem (v139)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-140 — Simplify Unix path (v140)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-141 — Sort a stack using recursion (v141)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-142 — Reverse a string using stack (v142)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-143 — Stock span problem (v143)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-144 — Celebrity problem (v144)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-145 — Implement queue using stacks (v145)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-146 — Trapping rain water using stack (v146)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-147 — Daily temperatures (next warmer) (v147)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-148 — Remove K digits for smallest number (v148)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-149 — Decode string with nested brackets (v149)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-150 — Asteroid collision simulation (v150)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-151 — Basic calculator (with +, -, (, )) (v151)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-152 — Remove duplicate letters (v152)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-153 — 132 pattern detection (v153)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-154 — Maximal rectangle (histogram extension) (v154)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-155 — Score of parentheses (v155)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-156 — Validate stack sequences (v156)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-157 — Minimum add to make parentheses valid (v157)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-158 — Exclusive time of functions (v158)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-159 — Car fleet problem (v159)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-160 — Simplify Unix path (v160)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-161 — Sort a stack using recursion (v161)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-162 — Reverse a string using stack (v162)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-163 — Stock span problem (v163)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-164 — Celebrity problem (v164)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-165 — Implement queue using stacks (v165)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-166 — Trapping rain water using stack (v166)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-167 — Daily temperatures (next warmer) (v167)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-168 — Remove K digits for smallest number (v168)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-169 — Decode string with nested brackets (v169)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.14a-170 — Asteroid collision simulation (v170)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.