Skip to content

Commit

Permalink
Min Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed Jul 19, 2024
1 parent 066b9fa commit 1a91a39
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0128 | Medium | Longest Consecutive Sequence | Array, HashSet | [solution](./docs/0128-Longest-Consecutive-Sequence.md) |
| 0150 | Medium | Evaluate Reverse Polish Notation | Array, Math, Stack | [solution](./docs/0150-Evaluate-Reverse-Polish-Notation.md) |
| 0155 | Medium | Min Stack | Stack, Design | [solution](./docs/0155-Min-Stack.md) |
| 0167 | Medium | Two Sum II - Input Array Is Sorted | Array, Two Pointers | [solution](./docs/0167-Two-Sum-II-Array-Is-Sorted.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
Expand Down
46 changes: 46 additions & 0 deletions docs/0155-Min-Stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# [Min Stack](https://leetcode.com/problems/min-stack/description/)

## Intuition

The goal of this problem is to design a stack that supports standard operations (`push`, `pop`, `top`) along with an
additional operation (`getMin`) which retrieves the minimum element in the stack. All operations should have `O(1)` time
complexity. To achieve this, we need to maintain a structure that can efficiently track the minimum value as elements
are pushed and popped from the stack.

## Approach

To implement the `MinStack`class, we use a custom linked list where each node stores three pieces of information:

1. The value of the element.
2. The minimum value in the stack up to this node.
3. A reference to the next node in the stack.

**Node Structure**

Each node is represented as a record with three fields:

- `val`: the value of the element.
- `min`: the minimum value in the stack up to and including this element.
- `next`: a reference to the next node in the stack.

**Stack Operations**

1. **Initialization (`MinStack`):** The stack is initialized with headset to null.
2. **Push (`push(int val)`)**
- When pushing a new value, a new node is created.
- If the stack is empty (`head` is `null`), the new node’s `min` value is the value being pushed.
- Otherwise, the new node’s `min` value is the minimum of the current head’s `min` value and the new value.
- The new node is then set as the new head of the stack.
3. **Pop (`pop()`)$:** The top element is removed by setting `head` to `head.next`.
4. **Top (`top()`):** Returns the value of the `head` node.
5. **Get Minimum (`getMin()`)** Returns the minimum value of the head node.

## Complexity

- **Time Complexity: O(1)** for each operation because they involve simple assignments and comparisons.
- **Space Complexity: O(N)**, where `N` is the number of elements in the stack, because each element is stored with its
associated minimum value.

## Code

- [Java](../src/main/java/io/dksifoua/leetcode/minstack/MinStack.java)
32 changes: 32 additions & 0 deletions src/main/java/io/dksifoua/leetcode/minstack/MinStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.dksifoua.leetcode.minstack;

public class MinStack {

private record Node(int val, int min, Node next) {}

private Node head;

public MinStack() {
head = null;
}

public void push(int val) {
if (head == null) {
head = new Node(val, val, null);
} else {
head = new Node(val, Math.min(head.min(), val), head);
}
}

public void pop() {
head = head.next();
}

public int top() {
return head.val();
}

public int getMin() {
return head.min();
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/dksifoua/leetcode/minstack/MinStackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.dksifoua.leetcode.minstack;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MinStackTest {

private MinStack minStack;

@BeforeEach
void setUp() {
minStack = new MinStack();
}

@Test
void test() {
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
Assertions.assertEquals(-3, minStack.getMin());
minStack.pop();
Assertions.assertEquals(0, minStack.top());
Assertions.assertEquals(-2, minStack.getMin());
}
}

0 comments on commit 1a91a39

Please sign in to comment.