【懒删除堆 优先队列】1172. 餐盘栈

本文涉及知识点

懒删除堆 优先队列

LeetCode1172. 餐盘栈

我们把无限数量 ∞ 的栈排成一行,按从左到右的次序从 0 开始编号。每个栈的的最大容量 capacity 都相同。
实现一个叫「餐盘」的类 DinnerPlates:
DinnerPlates(int capacity) - 给出栈的最大容量 capacity。
void push(int val) - 将给出的正整数 val 推入 从左往右第一个 没有满的栈。
int pop() - 返回 从右往左第一个 非空栈顶部的值,并将其从栈中删除;如果所有的栈都是空的,请返回 -1。
int popAtStack(int index) - 返回编号 index 的栈顶部的值,并将其从栈中删除;如果编号 index 的栈是空的,请返回 -1。

示例:

输入:
[“DinnerPlates”,“push”,“push”,“push”,“push”,“push”,“popAtStack”,“push”,“push”,“popAtStack”,“popAtStack”,“pop”,“pop”,“pop”,“pop”,“pop”]
[[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]]
输出:
[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1]

解释:
DinnerPlates D = DinnerPlates(2); // 初始化,栈最大容量 capacity = 2
D.push(1);
D.push(2);
D.push(3);
D.push(4);
D.push(5); // 栈的现状为: 2 4
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // 返回 2。栈的现状为: 4
1 3 5
﹈ ﹈ ﹈
D.push(20); // 栈的现状为: 20 4
1 3 5
﹈ ﹈ ﹈
D.push(21); // 栈的现状为: 20 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(0); // 返回 20。栈的现状为: 4 21
1 3 5
﹈ ﹈ ﹈
D.popAtStack(2); // 返回 21。栈的现状为: 4
1 3 5
﹈ ﹈ ﹈
D.pop() // 返回 5。栈的现状为: 4
1 3
﹈ ﹈
D.pop() // 返回 4。栈的现状为: 1 3
﹈ ﹈
D.pop() // 返回 3。栈的现状为: 1

D.pop() // 返回 1。现在没有栈。
D.pop() // 返回 -1。仍然没有栈。

提示:

1 <= capacity <= 20000
1 <= val <= 20000
0 <= index <= 100000
最多会对 push,pop,和 popAtStack 进行 200000 次调用。

优先队列

有以下三类栈可以push:
一,从没操作过的栈。m_opeCnt 记录push过的栈数量。m_opeCnt就会第一个没push过的栈。
二,push过,但没满的栈。
三,pop过的栈。
第二类和第三类栈用小根堆m_heapCanPush记录。
以下栈可以pop:
push过的栈,用大根堆m_headCanPush记录。
由于只能删除栈顶元素,所有要删除的元素,在出栈的时候删除。

代码

class DinnerPlates{
public:
	DinnerPlates(int capacity) :m_iCapacity(capacity) {

	}

	void push(int val) {
		while (m_heapCanPush.size() && (m_stas[m_heapCanPush.top()].size()== m_iCapacity)) {
			m_heapCanPush.pop();
		}
		int index = m_opeCnt;
		if (m_heapCanPush.size() && (m_heapCanPush.top() < index )) {
			index = m_heapCanPush.top();
		}
		else {
			m_opeCnt++;
		}
		m_stas[index].push(val);
		Fresh(index);
	}

	int pop() {
		while (m_heapCanPop.size() && (m_stas[m_heapCanPop.top()].empty())) {
			m_heapCanPop.pop();
		}
		if (m_heapCanPop.empty()) {	return -1;	}
		return Pop(m_heapCanPop.top());
	}

	int popAtStack(int index) {
		if (m_stas[index].empty()) { return -1; };
		return Pop(index);
		
	}
protected:
	int Pop(int index) {
		int ret = m_stas[index].top();
		m_stas[index].pop();
		Fresh(index);
		return ret;
	}
	void Fresh(int index) {
		if (m_stas[index].size() != m_iCapacity) {
			m_heapCanPush.emplace(index);
		}
		if (!m_stas[index].empty()) {
			m_heapCanPop.emplace(index);
		}
	}
	const int m_iCapacity;
	priority_queue<int, vector<int>, std::greater<>> m_heapCanPush;
	priority_queue<int> m_heapCanPop;
	stack<int> m_stas[200'000];
	int m_opeCnt = 0;
};

扩展阅读

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关推荐

我想对大家说的话
喜缺全书算法册》以原理、正确性证明、总结为主。
按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

  • 25
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻缺陷则喜何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值