How would you design a stack which, in addition to push and pop, also has a function
min which returns the minimum element? Push, pop and min should all operate in
O(1) time.
public class StackWithMin extends Stack<NodeWithMin> {
public void push(int value) {
int newMin = Math.min(value, min());
super.push(new NodeWithMin(value, newMin));
}
}
public int min() {
if (this.isEmpty()) {
return Integer.MAX_VALUE;
} else {
return peek().min;
}
}
class NodeWithMin {
public int value;
public int min;
public NodeWithMin(int v, int min){
value = v;
this.min = min;
}
}
分享到:
相关推荐
栈, 最小值
Hands-On Full-Stack Development with Swift 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
《jQuery响应式可拖拽的元素组件网格布局插件Gridstack.js详解》 在Web开发领域,用户界面的交互性和可定制性越来越受到重视。Gridstack.js是一款基于jQuery的响应式可拖拽的元素组件网格布局插件,它提供了一种...
本源码实现了两个类,一个是带有最大值的栈和一个是带有最大值的队列。栈利用了两个C++的stack,队列利用了C++的queue。
Using Vapor, we will build a full-stack web application that will act as an API server for our iOS and tvOS app, and will also be our web server, which will render a web view of our app. Several ...
Hands-On Full Stack Development with Go - Mina Andrawos(2019),Go全栈开发,epub格式,欢迎下载
C语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 ...
### Z-Stack操作系统详解 #### 一、Z-Stack操作系统简介 Z-Stack是一个专为ZigBee网络设计的操作系统,它支持IEEE 802.15.4标准和ZigBee协议栈。Z-Stack的核心是OSAL(Operating System Abstraction Layer),这是...
Full Stack Web Development with Raspberry Pi 3 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Building Hybrid Clouds with Azure Stack 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
在汇编语言的学习中,"STACK1_SEGMENT_STACK.rar_STACK1 SEGMENT_stack segment stack"这个标题提到了两个关键概念:栈段(Stack Segment)和栈(Stack)。栈在计算机科学中扮演着至关重要的角色,尤其是在汇编语言...
Full Stack JavaScript Development With MEAN 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Write Modern Web Apps with the MEAN Stack Mongo Express AngularJS and Node.js 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
JEDEC JEP158:2009 3D Chip Stack with Through-Silicon Vias (TSVS):Identifying, Evaluating and Understanding Reliability Interactions - 完整英文电子版(21页).zip
Create a RESTful web service with Spring Boot Use React for frontend programming Learn to create unit tests using JUnit Discover techniques to secure the backend using Spring Security Employ Material-...