当前位置:网站首页>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
边栏推荐
- How to write a software test report? Here comes the third party performance report template
- Multi project design and development · introduction to class library project
- Introduction to multi project development, basic design class library project use
- Reverse a stack with recursive functions and stack operations only
- first. Net core MVC project
- 02 MongoDB数据类型、重要概念以及shell常用指令
- 抖音實戰~關注博主
- 利用ELK 搭建日志分析系统(二)—— 部署安装
- GenICam GenTL 标准 ver1.5(2)
- One article tells you what kubernetes is
猜你喜欢

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

Arrangement of basic electrical knowledge (II)

美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书

Staggered and permutation combination formula

How to learn a programming language systematically| Dark horse programmer

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

@Several scenarios of transactional failure

Web APIs DOM event foundation dark horse programmer

抖音實戰~關注博主

applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
随机推荐
基于arm5718的Shell脚本参数传递的2种方法
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
MSc 307 (88) (2010 FTPC code) Part 9 bedding test
猫狗队列的问题
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
Zipkin service link tracking
关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
抖音实战~关注博主
GenICam GenTL 标准 ver1.5(2)
从零到一,教你搭建「以文搜图」搜索服务(一)
Pychart shares third-party modules among different projects
Backtracking maze problem
English notes - cause and effect
PostgreSQL 实现批量更新、删除、插入
Two methods of shell script parameter passing based on arm5718
单一职责原则
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
第一个.net core MVC项目
04 summary of various query operations and aggregation operations of mongodb