当前位置:网站首页>Design a stack with getmin function
Design a stack with getmin function
2022-06-28 04:13:00 【The roaring Conan】
Design one with getMin Stack of functions
【 subject 】
Implement a special stack , On the basis of realizing the basic functions of stack , The operation of the smallest element in the return stack .
【 requirement 】
1.pop、push、getMin The time complexity of operation is O(1).
2. The designed stack type can use the existing stack structure .
【 Ideas 】
Use two stacks , A stack a Normal access , Another stack b Used to maintain the minimum value , Always keep b The top of the stack is the current a The minimum value in the stack .
Method 1 【 The depth of the two stacks is inconsistent 】: When entering the stack , Each entry a When the stack , If b The stack is empty. , Enter into a The stack enters at the same time b Stack , If you enter a Stack value ratio b The top of the stack is big , No entry , conversely , Get into b Stack , Update minimum ; Out of the stack , If the stack value is equal to b To the top of the stack , Just put b At the top of the stack , Update minimum , conversely ,b Stack does not update .
give an example : Press in in turn 3、4、5、1、2、1 In the process of ,stackData(a) and stackMin(b) The changes are shown in the figure below .
explain : The picture comes from Zuoshen's program code interview guide , For learning purposes only .

Method 2 【 The two stacks have the same depth 】: When entering the stack , Get into a Stack time , If b The stack is empty. , Get into b Stack , If b Stack is not empty and b Stack top less than a Stack new stack value , Then put the b The top of the stack is pushed again
边栏推荐
- 多项目设计开发·类库项目引入入门
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- 僅用遞歸函數和棧操作逆序一個棧
- @Several scenarios of transactional failure
- MySQL master-slave replication, separation and resolution
- 成长一夏 挑战赛来袭 | 学习、创作两大赛道,开启导师报名啦!
- 简单工厂模式
- What is the process of en 1101 flammability test for curtains?
- Component splitting practice
- MSc 307 (88) (2010 FTPC code) Part 5 low flame spread test
猜你喜欢

抖音实战~取关博主

云厂商为什么都在冲这个KPI?

ambari SSLError: Failed to connect. Please check openssl library versions.

抖音實戰~關注博主

公司领导说,个人代码超10个Bug就开除,是什么体验?

2022年6月对自己近况的一次总结

有关函数模板的那些小知识-.-

错排兼排列组合公式

In the era of video explosion, who is supporting the high-speed operation of video ecological network?

A preliminary study of blackbody radiation
随机推荐
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Secouer le son et se battre ~ prêter attention au blogueur
05 MongoDB对列的各种操作总结
01 overview, application scenarios, Download methods, connection methods and development history of mongodb
PostgreSQL implements batch update, deletion and insertion
Door level modeling - learning notes
指针链表
Reading notes of top performance version 2 (II) -- Performance observation tool
Lingge leangoo agile Kanban tool adds the functions of exporting card documents and pasting shared brain map nodes
Another option for ERP upgrade, MES system
Conversion between decimal and BCD codes in C language
How to write a software test report? Here comes the third party performance report template
leetcode - 329. 矩阵中的最长递增路径
Learning notes of digital circuit (II)
利用ELK 搭建日志分析系统(一)—— 组件介绍
Pychart shares third-party modules among different projects
Two methods of shell script parameter passing based on arm5718
From zero to one, I will teach you to build a "search by text and map" search service (I)
GenICam GenTL 标准 ver1.5(2)
美创入选“2022 CCIA中国网络安全竞争力50强”榜单