算法(TS):最小栈

最小栈

设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

实现 MinStack 类:

  • MinStack() 初始化堆栈对象。
  • void push(int val) 将元素val推入堆栈。
  • void pop() 删除堆栈顶部的元素。
  • int top() 获取堆栈顶部的元素。
  • int getMin() 获取堆栈中的最小元素。

提示:

  • -231 <= val <= 231 - 1
  • pop、top 和 getMin 操作总是在 非空栈 上调用
  • push, pop, top, and getMin最多被调用 3 * 104 次

解答一

用一个栈同时保持入栈的值和当前栈的最新值

ts 复制代码
class MinStack {
    private data: {val: number;min:number}[] = []
    constructor() {
        
    }

    push(val: number): void {
        
        if (this.data.length === 0) {
            this.data.push({
                val,min:val
            })
        } else {
            const top = this.data[this.data.length-1]
            this.data.push({
                val,
                min: Math.min(val,top.min)
            })
        }
        
    }

    pop(): void {
        this.data.pop()
    }

    top(): number {
        return this.data[this.data.length-1].val
    }

    getMin(): number {
        return this.data[this.data.length-1].min
    }
}

解答二

用一个链表,链表中的每个节点保存到当前位置最小的值。

ts 复制代码
interface ValNode {
    val: number;
    min: number;
    next: ValNode | null
}

class MinStack {
    private head: ValNode | null = null
    constructor() {
        
    }

    push(val: number): void {
        if (this.head) {
            const prevHead = this.head
            this.head = {
                val,
                min: Math.min(prevHead.min,val),
                next:prevHead
            }
        } else {
            this.head = {
                val,
                min:val,
                next: null
            }
        }
    }

    pop(): void {
        if(this.head) {
            this.head = this.head.next
        }
    }

    top(): number {
        return this.head.val
    }

    getMin(): number {
        return this.head.min
    }
}

解答三

用单独的变量 min 表示栈在目前位置的最小值,维护一个 diff 栈,其中保存入栈的值与 min 的差值。

ts 复制代码
class MinStack {
    private diff: number[] = []
    private min: number | undefined
    constructor() {
        
    }

    push(val: number): void {
        if(this.diff.length === 0) {
            this.min = val
            this.diff.push(0)
        } else {
            const diff = val - this.min
            this.diff.push(diff)
            // 入栈的值更小了
            if (diff < 0) {
                this.min = val
            } 
        }
    }

    pop(): void {
        const diff = this.diff.pop()
        if(diff < 0) {
            this.min = this.min - diff
        }
    }

    top(): number {
        const diff = this.diff[this.diff.length-1]
        if(diff < 0) {
            return this.min
        } else {
            return diff + this.min
        }
    }

    getMin(): number {
        return this.min
    }
}
相关推荐
眼镜哥(with glasses)35 分钟前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
int型码农5 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT5 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面5 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked936 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,6 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵6 小时前
有效的括号题解
数据结构·算法·
GIS小天6 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__6 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃6 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵