From da66c8ed675db563996d2d75db19f8808a4a829f Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Thu, 10 Apr 2025 22:09:29 +0800 Subject: [PATCH] :sparkles: (solution): Problem 155. Min Stack Add solution of 155. Min Stack --- solutions/155. Min Stack/README.md | 73 ++++++++++++++++++++++++++ solutions/155. Min Stack/Solution.java | 21 ++++++++ solutions/155. Min Stack/Solution.py | 20 +++++++ 3 files changed, 114 insertions(+) create mode 100644 solutions/155. Min Stack/README.md create mode 100644 solutions/155. Min Stack/Solution.java create mode 100644 solutions/155. Min Stack/Solution.py diff --git a/solutions/155. Min Stack/README.md b/solutions/155. Min Stack/README.md new file mode 100644 index 0000000..84d01e6 --- /dev/null +++ b/solutions/155. Min Stack/README.md @@ -0,0 +1,73 @@ +--- +comments: true +difficulty: medium +# Follow `Topics` tags +tags: + - Stack + - Design +--- + +# [155. Min Stack](https://leetcode.com/problems/min-stack/description/) + +## Description + +Create a special type of stack called MinStack that, in addition to the usual stack operations, can also return the minimum element currently in the stack — all in constant time (O(1)). + +The MinStack class should support the following operations: + +1. `MinStack()`: Initializes a new instance of the stack. + +2. `void push(int val)`: Adds the given value `val` to the top of the stack. + +3. `void pop()`: Removes the top element from the stack. + +4. `int top()`: Returns the element currently at the top of the stack. + +5. `int getMin()`: Returns the smallest element in the stack at any given time. + +Make sure that all these methods run in constant time. + + +**Example 1:** +``` +Input: +["MinStack","push","push","top","push","getMin","pop","top","getMin"] +[[],[-1],[3],[],[-2],[],[],[],[]] + +Output: [null,null,3,null,-2,null,3,-1] + +Explanation: +MinStack minStack = new MinStack(); +minStack.push(-1); +minStack.push(3); +minStack.top(); // return 3 +minStack.push(-2); +minStack.getMin(); // return -2 +minStack.pop(); +minStack.top(); // return 3 +minStack.getMin(); // return -1 +``` + +**Constraints:** + +* `-231 <= val <= 231 - 1` +* Methods `pop`, `top` and `getMin` operations will always be called on non-empty stacks. +* At most `3 * 10^4` calls will be made to `push`, `pop`, `top`, and `getMin`. + +## Solution + +Intuitively remove non-alphanumeric and change others to lowercase. + +```java +``` + +```python +``` + +## Complexity + +- Time complexity: $$O(1)$$ + + +- Space complexity: $$O(n)$$ + diff --git a/solutions/155. Min Stack/Solution.java b/solutions/155. Min Stack/Solution.java new file mode 100644 index 0000000..7e6359d --- /dev/null +++ b/solutions/155. Min Stack/Solution.java @@ -0,0 +1,21 @@ +import java.util.*; + +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for (char c : s.toCharArray()) { + if (c == '(' || c == '{' || c == '[') { + stack.push(c); + } else { + if (stack.isEmpty()) { + return false; + } + char top = stack.pop(); + if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) { + return false; + } + } + } + return stack.isEmpty(); + } +} diff --git a/solutions/155. Min Stack/Solution.py b/solutions/155. Min Stack/Solution.py new file mode 100644 index 0000000..55116f2 --- /dev/null +++ b/solutions/155. Min Stack/Solution.py @@ -0,0 +1,20 @@ +class MinStack: + + def __init__(self): + self.stack = [] + + def push(self, val: int) -> None: + top = self.stack[-1] if self.stack else -1 + min_val = top[1] if isinstance(top, tuple) else float('inf') + if val < min_val: + min_val = val + self.stack.append((val, min_val)) + + def pop(self) -> None: + self.stack.pop() + + def top(self) -> int: + return self.stack[-1][0] if self.stack else None + + def getMin(self) -> int: + return self.stack[-1][1] if self.stack else None