[Leetcode] 716. Max Stack 解题报告

leetcode不会-max-stack:最大堆栈 leetcode 不会最大堆栈 设计一个支持push、pop、top、peekMax和popMax的最大栈。 push(x) -- 将元素 x 推入堆栈。 pop()——移除栈顶元素并返回。 top() -- 获取顶部的元素。 peekMax()——检索堆栈中的最大元素。 popMax() -- 检索堆栈中的最大元素,并将其删除。 如果您发现多个最大元素,则只删除最上面的一个。 Example 1: MaxStack stack = new MaxStack(); stack.push(5); stack.push(1); stack.push(5); stack.top(); -> 5 stack.popMax(); -> 5 stack.top(); -> 1 stack.peekMax(); -> 5 stack.pop(); -> 1 stack.top(); -> 5 笔记: -1e7 <= x <= 1e7 操作次数不会超过 10000。 当堆栈为空时,不会调用最后四个操作。 执行 : class MaxStack { Stack< Integer > stack = ne 立即下载

题目:

Design a max stack that supports push, pop, top, peekMax and popMax.

  1. push(x) -- Push element x onto stack.
  2. pop() -- Remove the element on top of the stack and return it.
  3. top() -- Get the element on the top.
  4. peekMax() -- Retrieve the maximum element in the stack.
  5. popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

Example 1:

MaxStack stack = new MaxStack();
stack.push(5); 
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5

Note:

  1. -1e7 <= x <= 1e7
  2. Number of operations won't exceed 10000.
  3. The last four operations won't be called when stack is empty.

思路:

我们定义两个数据结构:1)list<int>用来模拟栈,这样可以实现push,pop和top;2)map<int, vector<list<int>::iterator>>,用来实现peekMax和popMax,因为我们可以从map中利用O(1)的时间复杂度得到最大的数,而由于我们在map中也同时保存了各个数对应的迭代器,所以也可以在O(1)的时间复杂度内删除掉list中的内容。

这样的话,可以保证push和pop的时间复杂度为O(logn),top,peekMax,popMax的时间复杂度都为O(1)。整个算法的空间复杂度为O(n)。我感觉应该是最优的算法了。

代码:

class MaxStack {
public:
    /** initialize your data structure here. */
    MaxStack() {
        
    }
    
    void push(int x) {
        auto it = l.insert(l.end(), x);
        m[*it].push_back(it);
    }
    
    int pop() {
        auto it = l.end();
        --it;
        int value = *it;
        m[*it].pop_back();
        if (m[*it].empty()) {
            m.erase((*it));
        }
        l.erase(it);
        return value;
    }
    
    int top() {
        return *(l.rbegin());
    }
    
    int peekMax() {
        return m.rbegin()->first;
    }
    
    int popMax() {
        auto it = m.rbegin()->second.back();
        int value = m.rbegin()->first;
        m.rbegin()->second.pop_back();
        if (m.rbegin()->second.empty()) {
            m.erase(m.rbegin()->first);
        }
        l.erase(it);
        return value;
    }
private:
    list<int> l;
    map<int, vector<list<int>::iterator>> m;
};

/**
 * Your MaxStack object will be instantiated and called as such:
 * MaxStack obj = new MaxStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.peekMax();
 * int param_5 = obj.popMax();
 */
leetcode-multi-language:716小伙伴的刷题笔记 Leetcode多语言 前言 我们的口号是:面向跳槽刷题!!! 既然写得好的题解仓库有很多,github搜leetcode能找到一堆(详见后文),但真正能发挥功效的唯有自己的坚持不懈。 资料 一些实用的github项目(题解,面试),整理如下: 电子书《 labuladong的算法小抄》 链接: https://pan.baidu.com/s/1WH9IgfZoLXj0KT0Sm_dWyw 提取码: 652e 刷题网站: 安利指数: :white_medium_star: :white_medium_star: :white_medium_star: :white_medium_star: :white_medium_star: ,,每道题有视频讲解,很清楚很省时。 建议 每道题最好记录下自己最初的思路,有些做不出的译文在看了题解后再回头比较,这样反思收获的更多吧。 【llf路径】 详见java目录下的 立即下载

相关推荐

在实战中学AI-车流量统计项目-YOLOV11车辆检测-YOLOV11模型的部署

考虑实际落地中部署推理资源消耗,速度与模型精度之间的均衡,同时,车辆属于比较通用场景的目标检测,我们将YOLOV11作为最终的模型选择,目前官方开源的模型基于coco数据集训练,里面已经包含了车辆相关类别,经测试,在监控视角下效果基本可用,所以,在落地过程中,我们先用这个模型来打通部署链路,后面再逐步优化模型性能。在Nvidia的硬件下做部署,部署框架优先选择官方框架TensorRT,下面重点讲述如何用TensorRT部署YOLOV11模型。

Aleo_优快云的博客 2166

[LeetCode] Max Stack 最大栈

  Design a max stack that supports push, pop, top, peekMax and popMax.   push(x) -- Push element x onto stack. pop() -- Remove the element on top of the stack and return it. top() -- Get the ...

weixin_34007291的博客 287

ArcGIS基础:城市交通网络构建及设施服务分析(一):道路数据处理与拓扑操作

首先需要加载数据, 1:选择这个数据库:,右键导入,选择导入单个要素,弹出“要素类至要素类” 2:输出位置(导入的数据是CAD格式数据)。 3:输出要素类为道路。 4:字段只保留道路类型字段。 这个操作是分类合并各级道路数据。 1:通过属性选择各类道路数据。 2:比如此类都是“干道”。 3:编辑器下来,然后合并即可。 得到如图所示,需要用到三个字段。 上面这个操作是打断道路相交线。 1:选中所有数据。 2:找到高级编辑中的打断相交线。 3:确定即可。 下面进行拓扑检查,虽然我们已经进行合并和打断相交.

yuanYW7556的博客 2万+

【Leetcode】716. Max Stack

题目地址: https://leetcode.com/problems/max-stack/ 用栈实现一个数据结构可以实现如下操作: 1、将数据进栈,出栈,并且按照FILO的原则; 2、查看栈顶,查看栈中最大值; 3、删掉栈中最大值。 基本思路是用两个栈,其中一个栈s1s_1s1​存原本的数据,另开一个单调栈s2s_2s2​,存所有遇到的最大值。这样即使在原栈中的最大值pop掉,也能通过在第二个栈...

数学、算法爱好者的博客 730

单细胞转录组数据分析全流程:从原始数据到细胞注释

本文详细介绍了单细胞转录组数据分析的全流程,从原始数据读取、环境搭建、数据质控与标准化,到降维聚类、细胞类型注释,以及高级质控与多样本整合。重点阐述了如何利用Seurat等工具进行数据处理,并通过标志基因比对与自动注释方法完成细胞身份鉴定,为研究者提供了一套清晰、可操作的分析指南。

coffee的博客 553

STM32 HAL库驱动BMP388:从寄存器配置到高精度气压温度采集

本文详细介绍了如何使用STM32 HAL库驱动BMP388气压温度传感器,实现高精度数据采集。内容涵盖从硬件连接到STM32CubeMX配置、关键寄存器解析、驱动源码编写,到核心补偿算法与海拔换算的完整流程,并提供了针对不同应用场景的优化配置与调试技巧,帮助开发者快速构建稳定可靠的测量方案。

weixin_29299807的博客 171

716. 最大栈

716. 最大栈 设计一个最大栈数据结构,既支持栈操作,又支持查找栈中最大元素。 实现 MaxStack 类: MaxStack() 初始化栈对象 void push(int x) 将元素 x 压入栈中。 int pop() 移除栈顶元素并返回这个元素。 int top() 返回栈顶元素,无需移除。 int peekMax() 检索并返回栈中最大元素,无需移除。 int popMax() 检索并返回栈中最大元素,并将其移除。如果有多个最大元素,只要移除 最靠近栈顶 的那个。 示例: 输入 ["MaxSt

不搞数学的汤老师 822

21天学会基于 Linux 的 NPU 固件开发--2.2 固件加载流程:request_firmware() 深度解析

本文深度解析Linux内核中NPU固件加载流程,重点剖析request_firmware()机制。内容涵盖:1)内核态与用户态的交互机制,包括同步/异步加载方式;2)NPU固件的特殊格式与加载逻辑;3)调试技巧与安全验证方案;4)提供最小化固件加载器实验。文章揭示了从文件系统到硬件初始化的完整链路,探讨了大模型时代面临的性能挑战与优化方案,如零拷贝加载技术,并提出了版本兼容性、断电恢复等关键问题的思考方向。

amy_mhd的博客 59

Leetcode 716: Max Stack

问题描述: 设计一个堆栈,支持除普通堆栈功能之外,查找最大值和移除最大值的功能 思路: 双堆栈,之前题目中遇到过。一个堆栈负责正常来往顺序,另外一个堆栈保持与第一个堆栈等高,且对应高度的值代表在这个高度上的最大值 代码如下: class MaxStack { Stack<Integer> normalStack; Stack<Integer> maxStack; /** initialize your data structure here. */

weixin_51401145的博客 318

【推荐100个unity插件】unity实现交互式火焰 —— Ignis

Ignis是一款Unity互动式消防系统插件,可轻松将物体转换为可燃物。支持固体、植被、蒙皮网格和地形树木等多种对象类型,提供火焰蔓延、熄灭等效果。安装需通过Unity Package Manager添加Visual Effects Graph,并按照指引转换目标物体材质。使用盒型碰撞体或网格计算燃烧区域,可自定义火焰参数和视觉效果。内置多种转换选项,如Flammable Vegetation、Flammable Skinned Mesh等,适用于不同场景需求。注意使用前需备份材质和地形数据,并提供性能优化

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2236

手把手教你学Simulink--双足机器人的反步控制方法仿真

本文介绍了在Simulink中基于反步控制方法进行双足机器人仿真研究的步骤。主要内容包括:1)准备工作,包括MATLAB/Simulink安装和相关工具箱配置;2)构建简化双足机器人模型;3)设计反步控制器,阐述其递归设计原理及Lyapunov函数构造方法;4)通过MATLAB Function块实现控制逻辑并连接模型;5)仿真参数配置和结果分析。文章强调反步控制适用于非线性系统,通过逐步构造虚拟控制律保证系统稳定性,并指出需要多次调试和查阅文献来优化控制效果。该教程为研究双足机器人运动控制提供了实用的仿真

MHD0815的博客 152

[数据结构与算法] LeetCode-716 Max Stack

LeetCode-716 Max Stack Design a max stack that supports push, pop, top, peekMax and popMax. 1. push(x) -- Push element x onto stack. 2. pop() -- Remove the element on top of the stack and return it. 3. top() -- Get the element on the top. 4. peekMax() --

mouliu6141的博客 335

pyradiomics使用——1. 医学影像的组学特征值(Radiomics Features)提取

文章目录1. 使用插件1.1 插件安装1.2 插件使用2. 使用python代码2.1 安装2.2 使用2.2.1 官方示例2.2.2 快速使用2.2.3 其它使用3. 组学特征字段意义说明 Github地址:https://github.com/AIM-Harvard/pyradiomics 官方文档地址:https://pyradiomics.readthedocs.io/en/latest/features.html 1. 使用插件 从pyradiomics的Github仓库中可以知道,pyradio

啷个哩个啷 7369

【LeetCode】716. Max Stack 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法双栈日期 题目地址:https://leetcode-cn.com/problems/max-stack/ 题目描述 Design a max stack that supports push, pop, top, peekMax and ...

负雪明烛 1266

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

[leetcode]716. Max Stack @ python

原题 Design a max stack that supports push, pop, top, peekMax and popMax. push(x) – Push element x onto stack. pop() – Remove the element on top of the stack and return it. top() – Get the element on th...

闲庭信步 1652

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode 716. Max Stack - 链表(Linked List)系列题27

题目要求设计一个堆栈除了支持一般堆栈具有的接口函数:push(), pop(), top(),还需要支持返回堆栈中的最大值:peekMax(),和弹出堆栈中的最大值:popMax()。 本题可以在系统自带的堆栈的基础上实现题目所要求的堆栈。如果不用系统自带的而是让我自己实现一个堆栈的话,通常我们会用双向链表来实现,因为双向链表的插入和删除操作的时间复杂度可以达到在O(1)。

hgq522的博客 406

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode #716 - Max Stack

题目描述: Design a max stack that supports push, pop, top, peekMax and popMax. 1. push(x) -- Push element x onto stack. 2. pop() -- Remove the element on top of the stack and return it. 3. top() -- Ge...

LawFile的博客 500

716 Max Stack

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto stack.pop() -- Remove the element on top of the stack and return it.top() -- Get the element on the ...

dongbeier的博客 537

[leetcode]716. Max Stack 最大栈

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto stack. pop() -- Remove the element on top of the stack and return it. top() -- Get the eleme...

weixin_30764137的博客 331

LeetCode-Algorithms-[Easy]716. 最大栈

public class MaxStack { Stack<Integer> s; int max; /** initialize your data structure here. */ public MaxStack() { s = new Stack<Integer>(); } public void push(int x...

AlphaCat的博客 505

【Leetcode】155. Min Stack

题目地址: https://leetcode.com/problems/min-stack/ 设计一个栈,可以实现push pop top和得到栈里的最小值,所有操作都O(1)O(1)O(1)。容易想到要用两个栈,一个来标记最小值。 法1:用一个栈同时标记最小值以及它出现的次数。 import java.util.Stack; public class MinStack { Stack&...

数学、算法爱好者的博客 242

Leetcode刷题(第155题)——最小栈

一、题目 二、示例 输入: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] 输出: [null,null,null,null,-3,null,0,-2] 解释: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.

weixin_47450807的博客 1025

算法通关村-----栈的经典问题解析

栈的经典问题解析,包括括号匹配,最大栈,最小栈

今天不coding的博客 601

【LeetCode 栈】最大栈(716)

【LeetCode 栈】最大栈(716)

weixin_37780776的博客 1358

leetcode:716. 最大栈

使用两个栈来模拟,dataStack为普通的栈,用来保存所有的数字,而maxStack为最大栈,用来保存出现的最大的数字。

OceanStar的博客 310
上一篇: [Leetcode] 715. Range Module 解题报告
下一篇: [Leetcode] 717. 1-bit and 2-bit Characters 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值