惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
N
News and Events Feed by Topic
博客园 - Franky
Spread Privacy
Spread Privacy
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
博客园 - 叶小钗
S
Securelist
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
Vulnerabilities – Threatpost
量子位
D
Docker
NISL@THU
NISL@THU
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
小众软件
小众软件
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Heimdal Security Blog
L
Lohrmann on Cybersecurity
IT之家
IT之家
Webroot Blog
Webroot Blog
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale

博客园 - Zero Lee

调用栈(call stack) 关于STL allocator Calculate maximum sum of any subarray set Calcuate power n of x recursively Convert one binary search tree to double-linked list 类模板的模板友元函数定义 一道百度的面试题解答 非printf形式的十六进制和二进制打印(雅虎面试题) 一道腾讯面试题 (转)C++中extern “C”含义深层探索 selection algorithm to select nth small elements based on partition 删除与某个字符相邻且相同的字符 产生全排列的方法解析 一组数的全排列和组合程序实现 求一个正整数的平方根程序实现 [转]多线程队列的算法优化 [转载] STL allocator的介绍和一个基于malloc/free的allocator的简单实现 如何将一片内存链接成链表 One simple counted object pointer
设计包含min函数的栈
Zero Lee · 2012-06-17 · via 博客园 - Zero Lee

定义栈的数据结构,要求添加一个min的函数,能够得到栈的最下元素。
要求函数min, push, pop的时间复杂度都是O(1)

 1 class stackmin {
 2     int data[N];
 3     int minidx[N];
 4     int top;
 5 public:
 6     stackmin()
 7         : top(-1)
 8     {
 9         ::memset(minidx, -1, sizeof(int)*N);
10         ::memset(data, 0, sizeof(int)*N);
11     }
12 
13     bool push(int d)
14     {
15         if (top < N-1)
16             data[++top] = d;
17         else
18             return false;
19         if (top==0)
20             minidx[top] = 0;
21         else if (data[minidx[top-1]] > d)
22             minidx[top] = top;
23         else
24             minidx[top] = minidx[top-1];
25         return true;
26     }
27     bool pop(int& d)
28     {
29         if (top<0)
30             return false;
31         else
32 
33             d = data[top];
34         minidx[top] = -1;
35         top--;
36         return true;
37     }
38     bool min(int& m)
39     {
40         if (top>=0)
41             m = data[minidx[top]];
42         else
43             return false;
44         return true;
45     }
46     void print()
47     {
48         for (int i = 0; i <= top; i++)
49             std::cout << data[i] << " ";
50         std::cout << "\n";
51     }
52 };
53 

借助另一个内部的数组,用来存储每次push元素之后的栈最小值的索引,便可很方便的在O(1)的时间内完成以上三种操作。一种空间换时间的方法。