[leetcode: Python]155.Min Stack

FlowJO X 流式补偿调整 请自行解决FlowJO X 新版软件安装问题,懒人可以如我可以某宝解决。 本部分着重解决下机后不方便在原始机器上调补偿或者原始数据恰好被删除的情况(例如本人)。 前期工作: 1 流式上机(注:电压在机器上需提前调节好,此处不列举上机步骤攻略) 2 数据:实验管(FCS) 3 数据:补偿管(FCS) 软件界面-补偿模块 1 启动软件如下图: 2 3 ... 阅读详情

题目:
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.
Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

方法一:性能85ms

class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []
        self.minStack = []


    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        self.stack.append(x)
        if len(self.minStack) == 0 or x <= self.minStack[-1]:
            self.minStack.append(x)



    def pop(self):
        """
        :rtype: void
        """
        if not self.isEmpty():
            if self.stack.pop() == self.minStack[-1]:
                self.minStack.pop()


    def top(self):
        """
        :rtype: int
        """
        if not self.isEmpty():
            return self.stack[-1]


    def getMin(self):
        """
        :rtype: int
        """
        if not self.isEmpty():
            return self.minStack[-1]

    def isEmpty(self):
        """
        :rtype: bool
        """
        return len(self.stack) < 1




# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

方法二:性能75ms

class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.mins = [] # store mins along data, it takes some extra space, but provides O(1) access of the minimum element
        self.data = []


    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        if len(self.data)==0: # if we add the first element it is the minimum
            self.mins.append(x)
        else: # if not, check whether the element being added is smaller that currently the minimum element, if yes it is a new minimum element
            if x<self.mins[-1]:
                self.mins.append(x)
            else:
                self.mins.append(self.mins[-1]) # if not the previous element is still minimum
        self.data.append(x)


    def pop(self):
        """
        :rtype: void
        """
        self.mins.pop(-1) # drop the last minimum
        return self.data.pop(-1)



    def top(self):
        """
        :rtype: int
        """
        return self.data[-1]


    def getMin(self):
        """
        :rtype: int
        """
        return self.mins[-1]



# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

我猜这里造成性能差异的原因是电脑的性能差异。

OpenHarmony Stage模型深度解剖:从Ability Kit到沙箱隔离的全链路底层原理 基于微内核架构,提供进程管理、内存管理、IPC通信等基础能力:包含V8引擎、ArkTS运行时、渲染引擎等核心组件:Ability Kit框架,提供应用生命周期管理、组件调度等能力:基于Stage模型的应用程序。 阅读详情

相关推荐

MicroPython ubluetooth 库详细介绍

MicroPython的ubluetooth库为蓝牙通信提供了强大的支持,使得开发者能够在MicroPython环境下轻松实现蓝牙设备之间的通信。其中ubluetooth库为MicroPython提供了强大的蓝牙通信功能,使得开发者能够轻松地在各种支持MicroPython的硬件平台上实现蓝牙设备之间的通信。它提供了一系列的函数和类,用于实现蓝牙设备的扫描、连接、数据传输等功能。- 上述代码中,首先创建了一个蓝牙服务和一个特征,然后可以通过read和write函数读取和写入特征值。# 处理连接成功事件。

m0_46311182的博客 1765

155. Min Stack堆栈最小值Python

设计一个堆栈,以支持在恒定时间内推送,弹出,顶部和检索最小元素。 push(x)-将元素x推入堆栈 pop()-删除堆栈顶部的元素。 top()-获取顶部元素。 getMin()-检索堆栈中的最小元素。 Input:["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output:[null,null,null,null,-3,nul...

weixin_44516745的博客 298

别再硬编码了!Flowable工作流动态审批人配置全解析(Spring Boot集成版)

本文深入解析Flowable工作流在Spring Boot中的动态审批人配置技术,通过角色、部门等变量实现审批流程的灵活设计。文章详细介绍了架构原理、前端配置映射、动态候选组解析及查询优化方案,帮助企业提升审批流程的适应性和维护效率。

weixin_30919571的博客 138

Python Leetcode 155. Min Stack

Python Leetcode 155. Min Stack class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.stack = [] self.minstack = [] def push(self, x): """ :type x: i

GhostintheCode的博客 320

OpenWRT端口转发与NAT环回冲突的排查与解决

本文详细解析了OpenWRT路由器中端口转发与NAT环回功能冲突导致的内网访问故障。通过生活化类比阐明两者工作原理,提供从基础诊断到内核参数调整、LuCI选项配置及手动编写iptables规则的多套解决方案,并给出进阶排查思路,帮助用户彻底解决内网无法通过公网地址访问服务的常见问题。

weixin_29243279的博客 518

Windows 11下WSL2安装CUDA避坑指南:从驱动版本匹配到环境变量配置

本文详细解析了在Windows 11的WSL2中搭建CUDA开发环境的完整流程与核心避坑要点。关键在于理解“驱动在Windows,工具链在Linux”的独特架构,并确保显卡驱动与CUDA Toolkit版本严格匹配。指南涵盖了从驱动验证、WSL-Ubuntu专用仓库安装、环境变量配置到GCC版本冲突、GPU算力不匹配等常见问题的解决方案,帮助开发者高效构建稳定的深度学习与高性能计算环境。

smartcontract5的博客 482

python算法技巧——堆栈练习及掌握

本文介绍了python堆栈的一些应用与练习,希望对你有所帮助~~ 1. 给予一个字符串内含( )、[ ]、{ },判断是否合法: 2. 设计一个Minstack类,完成下列工作: 3. 使用队列(queue)完成下列堆栈的操作: 4. 按下列要求比较两段内容是否相同: 5. 删除相邻且相同的字符:

weixin_53919192的博客 1576

昆仑通态屏幕制作(连载1)---接触篇

写在开始 背景:最近出了做QT、LORA和4G,还做了一个屏幕。以前做了一个迪文的串口屏幕,发现网上的教程很多,所以连载就不更新了,并且迪文屏幕的设置什么的比较简单,应该从网上都能找到对应的教程。而现在用的昆仑通态的屏幕属于嵌入式屏幕,至于为啥叫嵌入式,也不是很明白,反正就是需要用这个屏幕,嵌入式不嵌入式不重要了。重要的是能用,并且能实现串口屏幕不能实现的功能。所以选定昆仑通态的一款屏幕。 文章语言:我比较习惯用比较糙的语言介绍做的东西,有时候不太准备,但是为了方便理解,还请大家谅解。用...

weixin_45426095的博客 6747

155. Min Stack [easy] (Python)

题目链接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()

Coder_Orz的博客 3863

对工时评估的思考:当AI颠覆了coding效率

在AI时代,“人日”作为软件工时度量衡已然失效。开发的价值核心正从“编码实现”转向“深度思考”与“精准沟通”——即如何将人类的模糊需求精确地传达给AI。当最有价值的“思想工作”变得最难量化时,传统的项目管理范式随之动摇。我们必须超越对过程的算计,开始探索衡量思想、沟通与业务价值的新标尺。

架构师刘7的博客 1024

基于Verilog的8位CPU设计及UART扩展实现方案,包含完整的架构设计、核心代码实现和仿真验证方法

基于Verilog的8位CPU设计及UART扩展实现方案,包含完整的架构设计、核心代码实现和仿真验证方法

a_zxswer的博客 196

155. Min Stack -- python

题目: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...

数据分析 506

机器人控制基础:电机编码器常见类型

电机编码器是测量电机位置、速度和方向的传感器,主要分为光电、磁性和旋转变压器三类。光电编码器精度高但需防尘,适用于伺服系统;磁性编码器耐环境性强,常用于汽车电机;旋转变压器适应极端环境,多用于军工领域。按信号类型可分为增量式(成本低)和绝对式(断电保留位置)。选型需综合考虑精度、环境适应性及成本。

start_up_go的博客 479

[LeetCode]题解(python):155-Min Stack

题目来源:   https://leetcode.com/problems/min-stack/ 题意分析:   实现一个小的栈,包括初始化,push,pop,top,和getMin。 题目思路:   思路是用两个数组来处理。 代码(python): 1 class MinStack(object): 2 def __init__(...

weixin_33932129的博客 105

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

jre9下载 jdk9

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

leetcode python3 简单题155. Min Stack

**# 1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百五十五题 (1)题目 英文: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element...

从此做一个俗人 243

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode [栈] 155.Min Stack(C++和Python实现)

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 th...

咸鱼夏の翻身观察记录 384

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

润泽的博客 248

LeetCode 155. 最小栈(Min Stack)

易错点正确做法说明弹出主栈未同步辅助栈时也要 pop辅助栈每次都入栈所有元素应该只记录当前最小值(含重复)空栈判断不完整或越界访问注意所有访问[-1]前判断非空本题是栈的经典设计题,重点考查辅助栈思想所有操作需 O(1) 完成,推荐双栈结构或元组压栈方式实现是系统设计类题目的入门题之一,适合练习类封装、状态管理等技巧。

2501_91641025的博客 725

[leetcode] 155. Min Stack @ python

原题 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...

闲庭信步 366

LeetCode 155:最小栈 Min Stack

LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 x 推入栈中。 pop() -- 删除栈顶的元素。 top() -- 获取栈顶元素。 getMin() -- 检索栈中的最小...

173

Leetcode155 Min Stack use Python

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

HeisenbergTHUUCD的博客 198

leetcode 155.min-stack 最小栈 python3

时间:2020-5-19 题目地址:https://leetcode-cn.com/problems/min-stack/description/ 题目难度:Easy 题目描述: 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) —— 将元素 x 推入栈中。 pop()—— 删除栈顶的元素。 top()—— 获取栈顶元素。 getMin() —— 检索栈中的最小元素。 示例: 输入: ["MinStack","push","push",..

isabloomingtree的博客 237

leetcode 155. Min Stack 双栈 python3

一.问题描述 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...

CSerwangjun的博客 337

leetcode155-min stack最小栈 python

思路:  采用两个栈,且两个栈长度相同 self.result栈中存放元素 self.min栈中存放每次添加元素时result栈中元素的最小值。result栈和mi栈长度相同 注意:本方法采用两个栈长度相同的策略,即min栈中存放的是result栈中添加或删除元素后result栈中的最小值。若采用min栈中只存放最小值(遇到大的值min栈不更新),则此时MinStack.pop()不好...

yzy的博客 283

【leetcode】(python)155. Min Stack最小栈

最小栈DescriptionExample题意解体思路code 155. Min Stack Easy Description Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. ...

。 264
上一篇: [leetcode: Python]141.Linked List Cycle
下一篇: [leetcode: Python]160. Intersection of Two Linked Lists
AliceWanderAI
博客等级 码龄10年 100粉丝 276原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值