当前位置:网站首页>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
边栏推荐
- 【MySQL】多表连接查询
- @Transactional失效的几种场景
- Uncertainty principle
- How to apply for ASTM E108 flame retardant test for photovoltaic panels?
- Another option for ERP upgrade, MES system
- 2021年终总结及2022年展望
- 03 summary of various additions, updates and deletions of mongodb documents
- What is the process of en 1101 flammability test for curtains?
- 【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
- 《性能之巅第2版》阅读笔记(二)--CPU监测
猜你喜欢

2021年终总结及2022年展望

Particle theory of light (photoelectric effect / Compton effect)

@Several scenarios of transactional failure
![[small program practice series] e-commerce platform source code and function implementation](/img/a4/581d73c2ff5c190edc3d8438fa2122.png)
[small program practice series] e-commerce platform source code and function implementation

Building a server monitoring platform with telegraf influxdb grafana

Introduction notes to machine learning

TFTLCD display experiment of mini plate based on punctual atom stm32

2021 year end summary and 2022 outlook

Open the field of maker education and creation

Analyzing the comprehensive application ability of educational robot
随机推荐
Chapter 14 AC-DC power supply front stage circuit note I
@Transactional失效的几种场景
Talking about cloud primitiveness, we have to talk about containers
单一职责原则
05 MongoDB对列的各种操作总结
揭开SSL的神秘面纱,了解如何用SSL保护数据
品达通用权限系统(Day 5~Day 6)
领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能
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
Zipkin service link tracking
02 MongoDB数据类型、重要概念以及shell常用指令
抖音实战~取关博主
光伏板怎么申请ASTM E108阻燃测试?
等保三级密码复杂度是多少?多久更换一次?
MSC 307(88) (2010 FTPC Code) Part 9床上用品试验
Chapter 1 Introduction to bash
Door level modeling - learning notes
回溯—迷宫问题
Web APIs DOM event foundation dark horse programmer
A preliminary study of blackbody radiation