Skip to content

Bug Report for minimum-stack #5627

@S-Stern

Description

@S-Stern

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.

Solution:

class MinStack:

    storage: List[int] = [] 
    _min_idx: List[int] = []

    def __init__(self):
        pass

    def push(self, val: int) -> None:
        self.storage.append(val)
        if not self._min_idx or self._min_idx[-1] > val:
            self._min_idx.append(val)
        else:
            self._min_idx.append(self._min_idx[-1])


    def pop(self) -> None:
        self.storage.pop()
        self._min_idx.pop()

    def top(self) -> int:
        return self.storage[-1]
        
    def getMin(self) -> int:
        return self._min_idx[-1]

Test cast:

["MinStack", "push", -1, "push", 3, "push", -4, "getMin", "pop", "getMin", "top"]

Output upon submission:

[null,null,null,null,-4,null,-2,3]

Output upon selecting test case and running:

[null,null,null,null,-4,null,-1,3]

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