LeetCode 155最小栈: 无额外占用空间-储存差值 |Min Stack with No extra space-Store as Difference

Problem description

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

The MinStack class:

  • Min Stack() initializes the stack object
  • void push(int val) pushes the element val onto stack
  • void pop() removes the element on the top of the stack
  • int top() get the top element of the stack
  • int getMin() retrieves the minimum element in the stack

Example:

Input: ["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output:
[null,null,null,null,-3,null,0,-2]

Normal method

The easy approach is to add a stack to store the minimum value of each element as the top of the stack in addition to the stack that stores the data.

class MinStack {
    private Deque stack;
    private Deque minStack;

    public MinStack() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
        minStack.push(Integer.MAX_VALUE);
    }

    public void push(int val) {
        stack.push(val);
        minStack.push(Math.min(val, minStack.peek()));
    }

    public void pop() {
        stack.pop();
        minStack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}

No extra space approach

What if we do not want(allowed) to use extra space to store the min value(minStack)?

Only store the difference in the stack! And store only one extra variable minValue.

Push to stack:
When the stack is empty, just store it, and assign it to minValue.

If the stack is not empty, we calculate the difference between the val and the minValue: diff=val-minValue, store it into the stack.

  • If the difference(diff) bigger than or equal to zero: diff>=0, then the entered value is greater than or equal to the minimum value in the stack, no need to update minValue.

  • If the difference(diff) is negative, then the entered value is less than minimum value in the stack, update the minValue.

Now let’s consider pop operation.

Pop from stack
When the stack is empty, operation fails.

If the stack is not empty.

  • If the peek value is non-negative. Then it doesn’t affect the minValue, just pop it.

  • If the peek value is negative. So we updated the minValue when pushing the value that make this peek difference, and the minValue is that val we want push to stack(call it new_minValue=val). So the old_minValue before push that element into stack should be old_minValue=new_minValue-diff.

top
Similar to pop.

If the stack is not empty:

  • If the peek value is non-negative. Then val=peekVall(diff)+minValue
  • If the peek value if negative. Then val=minValue.

min value
If the stack is not empty, return minValue.

It should be noticed that the differ operation may produce big integer that int can’t hold. Use Long or BigInteger instead.

Code as follows:

class MinStack {
    private Deque diffStack;
    private int minValue;

    public MinStack() {
        diffStack = new LinkedList<>();
        minValue = -1;
    }

    public void push(int val) {
        if (diffStack.isEmpty()) {
            diffStack.push(0L);
            minValue = val;
        } else {
            diffStack.push(Long.valueOf(val) - minValue);
            minValue = Math.min(minValue, val);
        }
    }

    public void pop() {
        Long diff = diffStack.pop();
        if (diff < 0) {
            minValue = (int) (minValue - diff);
        }
    }

    public int top() {
        Long diff = diffStack.peek();
        return diff >= 0 ? (int) (diff + minValue) : minValue;
    }

    public int getMin() {
        return diffStack.isEmpty() ? -1 : minValue;
    }
}

Original: https://www.cnblogs.com/liuzhch1/p/16403341.html
Author: liuzhch1
Title: LeetCode 155最小栈: 无额外占用空间-储存差值 |Min Stack with No extra space-Store as Difference

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606682/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • SpringBoot 2.1.9 整合 Redisson分布式锁

    官方参考文档 redisson-spring-boot-starter 官方文档 通过YAML文件配置单节点模式 一)、引入Redisson整合Spring Boot依赖 二)、通…

    Linux 2023年5月28日
    096
  • Ubuntu更换镜像源

    当修改 sources.list文件时,我们需要将下面任意一个镜像源的代码 复制粘贴到该文件中。 阿里源 阿里镜像源 deb http://mirrors.aliyun.com/u…

    Linux 2023年6月14日
    087
  • 十一、服务介绍及端口

    服务管理简介服务器的作用主要是什么?主要是通过网络来提供服务,比如apache提供一个web服务,mysql提供一个数据库服务,dns提供一个域名解析服务,ftp提供一个文件服务器…

    Linux 2023年6月7日
    0104
  • MSSQL中游标的语法结构

    | 0.21分钟 | 342.4字符 | 1、引言&背景 2、开箱即用的游标结构 3、声明与参考资料 | SCscHero | 2022/4/30 PM10:3 | 系列 …

    Linux 2023年6月14日
    084
  • Java实现两种队列(数组和链表)

    @date 2022-09-13 17:50*/public class QueueLinked{ private static class Node{E item;Node ne…

    Linux 2023年6月14日
    0125
  • 浅谈缓存击穿、缓存穿透、缓存雪崩、缓存预热、缓存降级

    对于缓存,大家肯定都不陌生,不管是前端还是服务端开发,缓存几乎都是必不可少的优化方式之一。在实际生产环境中,缓存的使用规范也是一直备受重视的,如果使用的不好,很容易就遇到缓存击穿、…

    Linux 2023年6月14日
    095
  • 阿里云IoT流转到postgresql数据库方案

    之前写过一篇如使用阿里云上部署.NET 3.1自定义运行时的文章,吐槽一下,虽然现在已经2022年了,但是阿里云函数计算的支持依然停留在.NET Core 2.1,更新缓慢,由于程…

    Linux 2023年6月6日
    0105
  • Jenkins 内置变量

    BRANCH_NAME 对于多分支项目,这将设置为正在构建的分支的名称,例如,如果您希望master从功能分支而不是从功能分支部署到生产;如果对应于某种更改请求,则名称通常是任意的…

    Linux 2023年5月27日
    096
  • SHELL编程-牛客网题目(持续更新..)

    SHELL编程题目及solution (牛客网) 描述:写一个 bash脚本以输出一个文本文件 nowcoder.txt中的行数示例:假设 nowcoder.txt 内容如下: #…

    Linux 2023年6月7日
    090
  • linux系统编码修改

    查看当前系统默认采用的字符集locale 查看系统当前编码echo $LANG如果输出为:en_US.UTF-8 英文zh_CN.UTF-8 中文 查看系统是否安装中文字符集loc…

    Linux 2023年6月6日
    091
  • 设计模式——-建造者模式(生成器模式)

    建造者模式(生成器模式)定义:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。 建造者模式中的4个角色: Product产品类 通常是实现了模板方法模式…

    Linux 2023年6月7日
    093
  • uniapp使用阿里云矢量图标库,h5端显示正常,真机app不显示问题解决

    1、在阿里云矢量图标库网站管理界面如上图,首先下载至本地1的位置,然后在2的位置复制代码 2、在下图中static目录下放入下载的iconfont.css文件,并且修改里面的链接,…

    Linux 2023年6月7日
    092
  • zabbix部署

    zabbix zabbix zabbix介绍 zabbix特点 zabbix部署 zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开…

    Linux 2023年6月13日
    0128
  • 服务管理与通信,基础原理分析

    涉及轻微的源码展示,可放心参考; 一、基础简介 服务注册发现是微服务架构中最基础的能力,下面将从源码层面分析实现逻辑和原理,在这之前要先来看下依赖工程的基础结构,涉及如下几个核心组…

    Linux 2023年6月14日
    0135
  • 安装及管理文件

    优点: 契合系统兼容性强 如果你可以看懂源代码,修改新增功能 比较自由 缺点: 如果编译出了问题,你看不懂源代码,无法解决 安装过程复杂 没有统一的管理人员 安装过程 程序包编译安…

    Linux 2023年6月6日
    092
  • C语言怎么给函数添加形参的默认值

    如果不是机缘巧合,当年转到C++之后,恐怕很难再有机会还写C的代码。面向对象在现代coding中,就像圣经一样,在码农的口中自带光环,code起来左一个语法糖,右一个范式编程,各种…

    Linux 2023年6月6日
    0105
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球