LeetCode Min Stack

等保测评-瀚高数据库 一般适用于等保测评 阅读详情

LeetCode Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.
此题关键部分就是需要在常量时间内返回栈中最小的元素,因此先把所有元素push栈,然后遍历找最小值不可取,因为时间复杂度为O(n)。
这里提出三种方法供参考。

方法一:用两个栈
一个栈用于正常的push和pop,另一个栈的栈顶保存当前最小数

class MinStack {

public:
	stack<int> s;
	stack<int> minStack;//栈顶元素为当前栈中最小元素
	

public:
	void push(int x) {
		if (minStack.empty()==true || x <= minStack.top())
			minStack.push(x);

		s.push(x);
	}

	void pop() {
		if (s.top() == minStack.top())
			minStack.pop();

		s.pop();
	}

	int top() {
		return s.top();
	}

	int getMin() {
		return minStack.top();
	}
};
此方法的getMin时间复杂度为O(1),空间复杂度O(n)


方法二:建立自己的数据结构,保存必要的信息

public:
<span style="white-space:pre">	</span>//自定义数据结构,保存需要的信息
	typedef struct element{
		int value;
		int min;
	}element;

	stack<element> s;
	
public:
	void push(int x) {
		int min = (s.empty() == true ? x : (s.top().min>x?x:s.top().min)); 

		element ele;
		ele.value = x;
		ele.min = min;
		s.push(ele);
	}

	void pop() {
		s.pop();
	}

	int top() {
		return s.top().value;
	}

	int getMin() {
		return s.top().min;
	}
显然,上面虽然减少了一个栈的使用,对效率有所提升,但是构建了结构体,保存了一些没必要的信息,如果结构体很大,依旧对内存有很大浪费,还有其他优化的方法吗??
能不能只用一个变量来保存当前最小值呢?


方法三:每次push新元素时,并不真正的push该元素,而是push该元素和栈中最小元素的差,这样就能随着元素push、pop而一直保存着最小元素。

class MinStack {
public:
	long min;//注意是long
	stack<long> s;//栈中的数据是long,因为push进来的数(如2^32)减去当前最小的数(-2^32)可能超过int的范围
	
public:
	void push(int x) 
	{
		if (s.empty() == true)
		{
			s.push(0);
			min = x;
		}
		else
		{
			s.push(x - min);//栈中保存要push的元素和min的差
			if (x < min)
				min = x;
		}
	}

	void pop() 
	{
		if (s.empty())
			return ;
        //退栈时需要更新min,s.top<0说明在push的时候就是最小值,因此此时pop了,需要更新min值
		long t = s.top();
		if (t < 0)
			min = min - t;

		s.pop();
	}

	int top() 
	{
	    //由于push的时候按照一定的方式改变了数据,因此需要把数据还原
		long t = s.top();
		if (t > 0)
		{
			return (int)(t + min);
		}		
		else
		{
			return (int)min;
		}
	}

	int getMin() 
	{
		return (int)min;
	}
};
此算法应该是比较优化了的。

【ArcGIS微课1000例】0118:一文讲清楚tif(geotiff)栅格数据格式 TIFF(Tagged Image File Format)是一种常见的图像文件格式,它被广泛用于存储和传输各种类型的图像数据。图像存储:TIFF格式可以存储多通道的位图图像,包括灰度图、彩色图和透明度图等。它支持不同的像素深度,可以是8位、16位、24位或32位等。因此,TIFF格式非常适合存储高质量的图像数据,包括照片、卫星图像、医学影像等。无损压缩:TIFF格式支持无损压缩,这意味着图像可以被压缩而无需损失任何像素信息。这种压缩方式可以减小文件大小,而不会降低图像的质量。 阅读详情

相关推荐

VSCode+Keil高效开发STM32:Keil Assistant插件配置全攻略

本文详细介绍了如何通过VSCode的Keil Assistant插件,将Keil MDK的编译调试能力与VSCode的现代编辑体验相结合,打造高效的STM32开发环境。文章涵盖了从环境配置、工程导入、智能提示设置到与STM32CubeMX协同工作的全流程攻略,旨在解决Keil编辑器效率低下的痛点,大幅提升嵌入式开发的编码与项目管理效率。

gold的博客 507

LeetCode[Stack]----Min Stack&Stack&Queue

整理一下LeetCode上三道与栈有关的题,分别是Implement Queue using Stacks,Implement Stack using Queues和Min Stack。 1.Implement Queue using Stacks Implement the following operations of a queue using stacks.

whiterbear的专栏 4218

手把手教你用闲置安卓手机搭建个人收款系统(蓝鲸支付私有化部署实战)

本文详细介绍了如何利用闲置安卓手机搭建个人收款系统,实现私有化支付部署。通过开源技术栈和容器化方案,零成本构建安全、高效的支付网关,解决传统支付平台的数据安全和资金风险问题。特别适合个人开发者和小微企业主,无需复杂审批,资金实时到账,显著降低运营成本。

weixin_30363285的博客 380

Min Stack -- LeetCode

Min Stack -- LeetCode

weixin_43418664的博客 80

STM32G0实战:手把手教你用国产GD25Q16 Flash存储传感器数据(附完整驱动)

本文详细介绍了如何使用国产GD25Q16 Flash芯片与STM32G0系列MCU实现传感器数据存储的完整方案。从SPI接口配置、驱动层实现到存储系统架构设计,提供了实战代码和优化策略,帮助开发者快速构建高性价比的物联网数据存储系统。

weixin_30733003的博客 106

保姆级教程:手把手教你用KITTI数据集跑通LIO-SAM(附EVO评估全流程)

本文提供了一份详细的KITTI数据集LIO-SAM实现教程,涵盖数据准备、算法适配和EVO评估全流程。通过解析KITTI数据集特性、解决LIO-SAM适配问题,并利用EVO工具进行精度评估,帮助读者快速掌握激光惯性SLAM技术。特别针对KITTI数据与LIO-SAM的兼容性问题提供了实用解决方案。

weixin_28737603的博客 303

Leetcode_Min Stack

设置两个栈,一个vbase用于存放原来的数据,一个minbase用于存放最小数据。将第一个进栈的数据同时压入两个栈中。在之后压入数据时,对于vbase,直接将数据压入即可;对于minbase,则需要比较待压入的数据与vbase顶端数据的大小,小于等于vbase顶端的数据,就将这个数据也压入minbase中。如果后来得到的值是大于当前最小栈顶的值的,那么接下来pop都会先出去,而最小栈顶的值会一直在

yinqiaohua的专栏 672

芒果YOLOv11改进175:DCNv4卷积,全网首发纯Pytorch实现代码,无需cuda编译|即插即用,一键运行:高效可变形卷积网络:重新思考视觉应用的动态和稀疏算子

芒果YOLOv11改进:DCNv4卷积,全网首发纯Pytorch实现代码,无需cuda编译|即插即用,一键运行:高效可变形卷积网络:重新思考视觉应用的动态和稀疏算子

包括YOLOv5、YOLOv7、YOLOv8等模型改进 1294

LeetCode力扣 155. 最小栈 Min Stack 题解代码 JavaScript

问题https://leetcode-cn.com/problems/min-stack/ 练习使用JavaScript解答 /** * initialize your data structure here. */ var MinStack = function() { this.stack = []; this.order = []; }; /** * @param {number} x * @return {void} */ MinStack.prototype..

Coding home - 漂流瓶jz 641

python实现马丁策略(股市) (附完整源码)

python实现马丁策略(股市) (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 603

基于STM32L4XX、HAL库的AD7124模数转换器ADC驱动程序设计

/ 检查ID是否正确 (AD7124-4/AD7124-8有不同的ID)// 转换为电压值 (假设使用内部2.5V基准)// 构造读命令: 1(R/W) + 1(0) + 6(寄存器地址)// 构造写命令: 0(R/W) + 1(0) + 6(寄存器地址).config = 0, // 使用配置0。内置可编程增益放大器(PGA):1-128倍。// 读取ADC数据。// 读取当前ADC控制寄存器。// 检查数据是否就绪。// 写回ADC控制寄存器。// 初始化AD7124。

zhusl6688的博客 519

LeetCode 155. Min Stack 题解

本题是栈的经典应用问题,主要考察对栈数据结构的理解和使用。通过使用辅助栈,我们可以在常数时间内检索到最小元素。辅助栈的核心思想是:使用两个栈,一个主栈用于存储所有元素,一个辅助栈用于存储当前的最小元素,当推入元素时,将当前最小元素压入辅助栈,当弹出元素时,同时弹出主栈和辅助栈的栈顶元素。这种方法不仅适用于最小栈问题,还可以应用于许多其他需要在常数时间内检索到特定元素的问题。掌握辅助栈的使用,对于解决这类问题非常重要。

cannonjinx的博客 4075

C语言关键字static的绝妙用途

为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量,在变量类型的前面加上就自动清0了,还有就是加上static关键字的,不管是变量还是关键字,在局部变量中,变量仅限于局部可见,在全局区中,static变量或函数仅限于本文件可见,当然还有一个重要的特性被忽略了。。。。       就是static修饰的局部变量 它的数值 是上一次函数调用结

Bruce.yang的嵌入式之旅 5376

leetcode 之Min Stack

Total Accepted: 15967 Total Submissions: 103378 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.po

TO TOP CODER 743

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode之Min Stack

题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() ...

qq_35455503的博客 146

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode-Min Stack

一共用两个栈。一个栈elements用来放所有数据,另一个栈mins专门用来存放最小值。入栈时所有元素都能加入elements栈,但只有当前元素小于等于mins栈的栈顶元素时才能加入mins栈。 1 class MinStack { 2 public: 3 void push(int x) { 4 elements.push(x); 5 ...

dongkai0918的博客 218

[LeetCode] Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the

楚兴 772

【Leetcode】Min Stack

题目链接:https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() --

业精于勤,荒于嬉 2331

leetcode Min Stack

题目连接 https://leetcode.com/problems/min-stack/ Min Stack Description Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element...

dong12276的博客 109

Leetcode Min_stack

实现能够取最小值得stack 原理:其中主栈和stack类似存取,另一个辅助栈在 新入值更小时 插入 当主栈弹出元素时,判断是否是辅助栈栈顶 若是 那么辅助栈同步弹出 class min_stack { private: stack<int> S; stack<int> S_min; public: void push(int a) { if (this->S_min.empty() || a <= S_min.top()) //当且仅当新入..

一只老风铃 329

Leetcode Min Stack 定义功能 python

Leetcode 155题 Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the...

润泽的博客 247

LeetCode——Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get

三分地 2147

[leetcode] 155. Min Stack

Description Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top...

Learning from the mistakes 257

leetcode: Min Stack

栈后进先出....对栈的操作仅限于push和pop,我们不妨再加一个stack   min_stack来维护栈的最小值列表(按照最小值更新的顺序,注意这里一定也要添加等于最小值的输入) class MinStack { Stack stack = new Stack(); Stack min_stack = new Stack(); publi

246

leetcode之Min Stack

Min Stackpush(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. class MinSta

到底意难平 584
上一篇: LeetCode Repeated DNA Sequences
下一篇: Binary Tree Maximum Path Sum
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值