LeetCode[stack]: Min Stack

LeetCode题解:Min Stack Stack(栈)是First in-Last out的数据结构。栈顶(top)指的是允许操作数据的一端,要与堆栈中高低地址不同的栈顶和栈底区别开来。 如果不考虑时间复杂度,实现题目的要求都比较简单,现在限定了不超过常量时间O(1), 就不能用简单的排序过滤实现了。 阅读详情

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.

Discuss上的这个解法:https://oj.leetcode.com/discuss/15679/share-my-java-solution-with-only-one-stack 非常巧妙,算法的关键在于用一个stack保存了当前元素与最小元素的差。为了避免Integer.MAXVALUE-Integer.MINVALUE这个异常出现,采用Long类型来保存。Java代码如下:

public class MinStack {
    long min;
    Stack<Long> stack;

    public MinStack(){
        stack=new Stack<>();
    }

    public void push(int x) {
        if (stack.isEmpty()){
            stack.push(0L);
            min=x;
        }else{
            stack.push(x-min);//Could be negative if min value needs to change
            if (x<min) min=x;
        }
    }

    public void pop() {
        if (stack.isEmpty()) return;

        long pop=stack.pop();

        if (pop<0)  min=min-pop;//If negative, increase the min value

    }

    public int top() {
        long top=stack.peek();
        if (top>0){
            return (int)(top+min);
        }else{
           return (int)(min);
        }
    }

    public int getMin() {
        return (int)min;
    }
}

这个算法的时间性能如下:

这里写图片描述

但是我在用C++实现相同的算法时却得到“Memory Limit Exceeded”,这是因为当用long类型时,其实无异于用两个stack。尽管如此,这个算法的思想还是值得学习的。

WIN下使用visual studio 2022安装GMSSL命令行工具 2.1 使用管理员权限打开visual studio(非常重要,否则最后一步 nmake install会失败)右键vs2022,以管理员身份运行即可。2.4 配置环境变量,gmssl可执行程序默认会被安装到如下目录。将如下路径添加到系统环境变量即可。添加后的环境变量如下。2.编译解压后的gmssl源码,编译过程主要参考install.md中的内容,README.md中的步骤不完整。2.2 打开vs2022的命令终端,右键文件,选择在终端中打开。1.下载gmssl源码,解压到相应目录。 阅读详情

相关推荐

子网划分详解

还不会子网划分?看这篇就够了,保姆级教程

m0_47584619的博客 6104

【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

跳出温水的青蛙 1万+

CCSv3.3安装避坑全记录:从补丁失败到XDS510驱动,手把手搞定DSP开发环境

本文详细记录了CCSv3.3安装过程中的常见问题及解决方案,包括补丁安装失败、XDS510驱动识别异常等。通过系统环境准备、安装路径优化、补丁顺序调整等步骤,手把手帮助开发者搭建稳定的DSP开发环境,特别适合高校DSP实验课使用。

weixin_33494995的博客 328

leetcode: Min Stack

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

247

逆向实战:拼多多anti-content加密参数的全链路解析与动态对抗策略

本文深入解析了拼多多anti-content加密参数的全链路生成逻辑与动态对抗策略。通过逆向工程实战,详细拆解了包含设备指纹、用户行为采集和动态密钥生成在内的12个关键环节,并提供了可持续对抗的精准模拟方案和自动化更新机制,帮助开发者有效应对拼多多的反爬体系。

weixin_30687051的博客 166

【Qt】Qt6系列教程汇总_qt6教程

简介:QT相关电子书、建议和相关参考网址。

2401_87556657的博客 1192

LeetCode:Min Stack

problem: 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.t

知行合一 止于行善 552

12V转5V稳压电路详细分析(7805三端稳压,LM2596稳压)

7805线性三端稳压 DCDC的意思是直流变到直流(不同直流电源值的转换),只要符合这个定义都可以叫DCDC转换器,或称之为开关电源或开关调整器。 7805属于常用的线性三端稳压器,输入电压范围不大于35V,输入输出压差2V,最大输出电流1.5A,下图为原理图,其原理简单,使 用方便,深受很多用户的喜爱。但是其不足之处就是效率低,特别是压差越大时其效率越低。比如这里12V转5V的效率为 5V/12V≈41.7%。 如上电路是最经典的7805稳压5V电路,电路中有两个电容,电容的作用如下: ...

a2001444的博客 9万+

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

恩,其实你很菜... 365

从翼型到湍流:用生活化案例图解空气动力学核心概念(中英双语版)

本文通过飞机机翼、F1赛车、高尔夫球等生活化案例,图解空气动力学核心概念。深入浅出地解释了攻角如何产生升力、边界层与流动分离如何影响阻力、马赫数如何划分速度世界,以及涡流与湍流的特性与应用。文章还介绍了计算流体动力学(CFD)这一现代数字风洞工具,帮助读者直观理解这些塑造现代交通与运动设计的无形之力。

metaverse5vr的博客 482

Python爬虫实战:基于最新技术栈的晋江文学城小说爬取

本文将详细介绍如何使用Python最新技术栈构建一个高效的晋江文学城小说爬虫。我们将从爬虫基础知识讲起,逐步深入到反爬机制应对、分布式爬虫设计以及数据存储优化等高级话题。文章包含完整的代码实现,使用aiohttp+asyncio实现异步爬取,Playwright处理动态渲染,并结合Redis实现分布式任务队列。本文旨在为读者提供一个全面的现代Python爬虫开发指南。网络爬虫(Web Crawler)是一种自动抓取互联网信息的程序,广泛应用于搜索引擎、数据分析和内容聚合等领域。

2201_76125261的博客 881

LeetCode155: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 t

898

真随机数与伪随机数

前面我们学习了LFSR,这个玩意落地到具体的应用是什么呢?没错是寄存器,这里在网络找到了几个回答,这里做个记录,下面一起来学习一下吧。

叫好与叫座虽然不是对立面,但想在同一个作品中达到双重效果很难。 1065

LeetCode 155: Min Stack

题目描述 思路 利用Stack,push的时候看是否造成最小值变化,如果最小值发生变化,push上一个最小值,改变最小值,不管是否改变最小值都push现在的值。pop的时候看是否造成最小值变化,(如果弹出的值是当前最小值说明最小值发生变化 ,进行判断的时候 弹出一次),再弹出一次更改min为前一个最小值,因为push的时候push了前一个最小值和现在的最小值。 java实现 class Min...

zero 164

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

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

给永远比拿愉快 900

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

《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 t

wojiushimogui的博客 572

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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 stack. top() – Get the ...

qq_34314736的博客 190

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question  Solution    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -...

weixin_33922672的博客 178

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()...

想做程序媛的小太阳 171

[C++]LeetCode: 26 Min Stack

题目:

cindy_niu的专栏 884

【Leetcode】:Min Stack

题目链接:https://oj.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(

懂幸福,爱生活 1015

leetcode:Min Stack

题目要求定义一个数据结构, class MinStack { ArrayList valueStack = new ArrayList<>(100000); ArrayList minValueStack = new ArrayList<>(100000); public void push(int x) { valueStack

469
上一篇: C++学习:vector用法
下一篇: LeetCode[String]: Longest Common Prefix
chfe910
博客等级 码龄14年 56粉丝 170原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值