Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Python solution for 155. Min Stack - Readability adjustment #3352

Open
Judeisbad opened this issue Mar 14, 2024 · 2 comments
Open

Comments

@Judeisbad
Copy link

The current solution is:

class MinStack:
    def __init__(self):
        self.stack = []
        self.minStack = []

    def push(self, val: int) -> None:
        self.stack.append(val)
        val = min(val, self.minStack[-1] if self.minStack else val)
        self.minStack.append(val)

    def pop(self) -> None:
        self.stack.pop()
        self.minStack.pop()

    def top(self) -> int:
        return self.stack[-1]

    def getMin(self) -> int:
        return self.minStack[-1]

within push, I think the current line is a little bit misleading as it's currently written as

val = min(val, self.minStack[-1] if self.minStack else val)

and writing it as

val = min(val, self.minStack[-1]) if self.minStack else val

is more readable to me.

Correct me if I'm wrong, thanks.

@mriitian
Copy link

The other version may indeed be slightly more readable to some due to its straightforward structure.

@zGadli
Copy link

zGadli commented Apr 11, 2024

True, your version is more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants