文件名称:
README.md
所在目录:
教材与参考资料 / 程序设计类 / 数据结构&算法 / 算法 / 面试与力扣题目详解 / Leetcode题目和解答 / java-leetcode / min-stack
文件大小:
672.00 B
下载地址:
文本预览:
## Constant time means to remember the min value
Update the min value when `push` and `pop`.
* push: easy! just update the min value if the new number is less than the current `min value`
* pop : bad ! need to rollback to a former min value if the current `min value` is poped
what we need is a non-ascending container for backing up the numbers.
```
MinStack = {
non_ascending_stack_for_rollback
stack_for_all
}
```
### The stack
[LinkedList](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) is a good one.
And it is easy to build your own stack.
Note: at first, I use array to build my stack, but `Arrays.copyOf` caused time limited.
Update the min value when `push` and `pop`.
* push: easy! just update the min value if the new number is less than the current `min value`
* pop : bad ! need to rollback to a former min value if the current `min value` is poped
what we need is a non-ascending container for backing up the numbers.
```
MinStack = {
non_ascending_stack_for_rollback
stack_for_all
}
```
### The stack
[LinkedList](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) is a good one.
And it is easy to build your own stack.
Note: at first, I use array to build my stack, but `Arrays.copyOf` caused time limited.
点赞
回复
X