-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/test/java/io/dksifoua/leetcode/minstack/MinStackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |