桟的min实现:O(1)时间复杂度
实现桟的push和pop操作,以及桟的min操作返回桟中的最小值,要求这三个操作的时间复杂度均为O(1)。
在Java中可以使用LinkedList实现桟的各种操作,这里使用双向链表实现桟的push和pop操作,这两个操作都能维持O(1)的时间复杂度,但是对于求桟中元素的最小值,最容易想到的方法是遍历整个桟,然后返回,但此时的时间复杂度为O(n),要想省下时间复杂度,则必须牺牲空间复杂度,所以可以再维护一个和桟大小相同的数据结构(可以是链表,动态数组或者一个新的桟)来存储每个元素入桟之后对应的桟中的最小元素,在对桟执行push和pop操作时,这个数据结构也跟着变化,这样就能以O(1)的时间复杂度实现min操作了。
public class Stack1<Key extends Comparable<Key>> {
private Node top;
private Node minTop;
private class Node {
Key key;
Node next;
Node prev;
Node(Key key, Node next, Node prev) {
this.key = key;
this.next = next;
this.prev = prev;
}
}
/* 元素入桟,因为要求桟的push,pop以及min操作都为O(1)操作,所以
* 在元素入桟时还要维护一个min桟 */
public void push(Key key) {
if (top == null) {
top = new Node(key, null, null);
top.next = null;
top.prev = null;
pushMin(key);
return;
}
Node x = new Node(key, null, top);
top.next = x;
top = x;
pushMin(key);
}
/* 维护一个最小元素的桟 */
private void pushMin(Key key) {
if (minTop == null) {
minTop = new Node(key, null, null);
minTop.next = null;
return;
}
if (key.compareTo(minTop.key) < 0) {
Node x = new Node(key, null, minTop);
minTop.next = x;
minTop = x;
} else {
Node x = new Node(minTop.key, null, minTop);
minTop.next = x;
minTop = x;
}
}
/* 元素出桟,因为要求桟的push,pop以及min操作都为O(1)操作,所以
* 在元素入桟时还要维护一个min桟 */
public Key pop() {
if (top == null) {
return null;
}
Key key = top.key;
if ((top.prev == null) && (top.next == null)) {
top = null;
popMin();
return key;
}
top = top.prev;
top.next = null;
popMin();
return key;
}
/* 返回桟中的最小元素 */
private Key popMin() {
if (minTop == null) {
return null;
}
Key key = minTop.key;
if ((minTop.prev == null) && (minTop.next == null)) {
minTop = null;
return key;
}
minTop = minTop.prev;
minTop.next = null;
return minTop.key;
}
/* 返回桟的最小元素 */
public Key min() {
/* 注意边界条件,当维护的桟为空时minTop也为空 */
if (minTop == null) {
return null;
}
return minTop.key;
}
public static void main(String[] args) {
Stack1<Integer> stack1 = new Stack1<Integer>();
stack1.push(11);
stack1.push(23);
stack1.push(4);
stack1.push(76);
stack1.push(13);
stack1.push(42);
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
System.out.println(stack1.min() + " " + stack1.pop());
}
}
上面main函数的输出为:
4 42
4 13
4 76
4 4
11 23
11 11
null null
桟的min实现:O(1)时间复杂度的更多相关文章
- 【老鸟学算法】包含 min函数的栈设计——java实现
要求: 1. 定义栈的数据结构,要求添加一个 min函数,能够得到栈的最小元素. 2. 要求函数 min.push 以及 pop 的时间复杂度都是 O(1). 这是考验“栈”数据结构设计.众所周知,栈 ...
- O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...
- 2.11 2D平面最近点对问题[closest pair problem]
[本文链接] http://www.cnblogs.com/hellogiser/p/closest-pair-problem.html [题目] 给定平面上N个点的坐标,找出距离最近的两个点之间的距 ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- 剑指offer系列18---顺时针打印矩阵
[题目]定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.要求时间复杂度是O(1).push pop min [思路]设计一个辅助栈,当新加入的原数据栈中的数小于辅助栈顶的数时就加 ...
- 剑指offer—第二章算法之二分查找(旋转数组的最小值)
旋转数组的最小数字 题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如:数组{3,4,5,1,2}为{1,2,3,4, ...
- AGC010 - D: Decrementing
原题链接 题意简述 给出一个个数的序列,足够聪明的AB两人轮流进行以下操作: 令一个大于1的数减1,然后所有数除以. 如果一个人不能操作了,那么他就输了. 输入保证所有数都是正整数并且. 分析 这是一 ...
- 剑指Offer--排序算法小结
剑指Offer--排序算法小结 前言 毕业季转眼即到,工作成为毕业季的头等大事,必须得认认真真进行知识储备,迎战笔试.电面.面试. 许久未接触排序算法了.平时偶尔接触到时自己会不假思索的百度,然后就是 ...
- 《剑指offer》内容总结
(1)剑指Offer——Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是统计和排序大量的字符串(但不仅限于字符串),所以经常 ...
随机推荐
- IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
CSS 2.1: IE 5.0 IE 5.5 IE 6.0 IE 7.0 IE8 Beta 1 IE8 Beta 2 IE 8.0 @charset No Yes Yes Yes Yes Yes ...
- Spring配置文件解析--集合注入方法
<bean id="moreComplexObject" class="example.ComplexObject"> <property n ...
- addViewController之后view里面的点击事件不响应
let dealsSeeMoreViewController = DealsSeeMoreViewController(owner: self) self.dealsStackView.addArra ...
- 数据类型转换的三种方式 Convert,parse和TryParse的解析
以Int类型为例,具体说明Convert.ToInt32(object value),int.Parse(object value)和int.TryParse(string s,out int res ...
- Python eclipse开发环境搭建
http://jingyan.baidu.com/article/cd4c2979101f02756f6e6064.html http://jingyan.baidu.com/article/1876 ...
- DataProcessing
clear load X4058 [m,n]=size(X528); Mean=zeros(1,n); Dev=zeros(1,n); for i=1:n Xi=X528(1:end-1,i); Xi ...
- 实例化Layout中的布局文件(xml)
什么是LayoutInflater This class is used to instantiate layout XML file into its corresponding View obje ...
- 上国际网络——通过配置host
http://laod.cn/hosts/2016-google-hosts.html
- Icon资源详解[1]
本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识,所有测试代码都尽可能的依赖win32 API实现.更全面的知识,参考文末列出的”参考资料“. 关键字:I ...
- easy_install jinja2 mac linux
error: can't create or remove files in install directory The following error occurred while trying t ...