Skip to content

Bug Report for minimum-stack: min should be maintained in a list #5836

@anil181

Description

@anil181

Bug Report for https://neetcode.io/problems/minimum-stack

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

The Problem with a Single Variable
Imagine you use a single integer variable self.min_val to track the minimum.

Let's walk through this sequence of operations:

Push 5: The stack is [5]. self.min_val = 5.

Push 3: The stack is [5, 3]. self.min_val updates to 3.

Push 7: The stack is [5, 3, 7]. self.min_val stays 3.

Now, let's call pop():

The 7 is removed. The stack is now [5, 3]. self.min_val is still 3. (This is correct).

Let's call pop() a second time:The 3 is removed. The stack is now just [5].The Crash: What should self.min_val be now? It should go back to being 5. But because 3 overwrote your single variable, your program has completely forgotten that 5 was the previous minimum!⚠️ The Rule: A single variable has no memory of history. When the current minimum is popped, you have no way of knowing what the next smallest number is without scanning the entire stack again, which forces a slow O(n)loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions