leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

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

Open Judeisbad opened this issue 1 year ago • 2 comments

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.

Judeisbad avatar Mar 14 '24 03:03 Judeisbad

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

mriitian avatar Mar 27 '24 09:03 mriitian

True, your version is more readable.

zGadli avatar Apr 11 '24 12:04 zGadli