当前位置:网站首页>LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
2022-07-03 09:20:00 【三岁就很萌@D】




List + 小顶堆 + 栈(双端队列实现的栈)




import java.util.*;
class DinnerPlates {
//list 栈 双端队列
//存放所有未满栈的下标的队列
private PriorityQueue<Integer> deque;
//按顺序存放所有栈
private List<Deque<Integer>> list;
//栈容量
private int capacity;
public DinnerPlates(int capacity) {
this.capacity = capacity;
//下标从小到大排序
deque = new PriorityQueue<Integer>(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i1.compareTo(i2);
}
});
list= new ArrayList<>();
}
public void push(int val) {
//如果当前存在没有满的栈
if(deque.size() > 0){
//取出从左至右第一个没有满的栈
Deque<Integer> stack = list.get(deque.peek());
//把val放入栈中
stack.offerFirst(val);
//如果栈满了
//将该栈从未满栈队列中弹出
if(stack.size() == this.capacity)
deque.poll();
}
else{
//当前栈都满了
//重新开一个栈
Deque<Integer> stack = new LinkedList<>();
//把val放入栈中
stack.offerFirst(val);
//将该栈放入栈队列中
list.add(stack);
//如果该栈没有满
if(stack.size() < this.capacity){
//将栈加入未满栈队列中
deque.offer(list.size()-1);
}
}
}
public int pop() {
//如果没有栈
if(list.size() == 0)
return -1;
int val = -1;
int index = list.size()-1;
//将list末尾的空栈删除
while(index >= 0 && list.get(index).size() == 0){
list.remove(index);
if(deque.contains(index))
deque.remove(index);
index -= 1;
}
if(index >= 0 && list.get(index).size() > 0) {
val = list.get(index).pollFirst();
if(!deque.contains(index))
deque.offer(index);
}
return val;
}
public int popAtStack(int index){
if(index > list.size() -1)
return -1;
if(list.get(index).size() == 0)
return -1;
int val = list.get(index).pollFirst();
if(!deque.contains(index))
deque.offer(index);
return val;
}
}
/** * Your DinnerPlates object will be instantiated and called as such: * DinnerPlates obj = new DinnerPlates(capacity); * obj.push(val); * int param_2 = obj.pop(); * int param_3 = obj.popAtStack(index); */
边栏推荐
- Octave instructions
- Emballage automatique et déballage compris? Quel est le principe?
- getopt_ Typical use of long function
- Not many people can finally bring their interests to college graduation
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Drive and control program of Dianchuan charging board for charging pile design
- el-table X轴方向(横向)滚动条默认滑到右边
- Application of external interrupts
- When the reference is assigned to auto
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
猜你喜欢

Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving

Opencv histogram equalization

Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)

Notes on C language learning of migrant workers majoring in electronic information engineering

Retinaface: single stage dense face localization in the wild

Opencv note 21 frequency domain filtering

Crash工具基本使用及实战分享

LeetCode - 673. 最长递增子序列的个数

Yocto technology sharing phase IV: customize and add software package support

LeetCode - 705 设计哈希集合(设计)
随机推荐
01仿B站项目业务架构
LeetCode - 673. 最长递增子序列的个数
Interruption system of 51 single chip microcomputer
Uniapp realizes global sharing of wechat applet and custom sharing button style
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Liquid crystal display
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Working mode of 80C51 Serial Port
01 business structure of imitation station B project
An executable binary file contains more than machine instructions
yocto 技術分享第四期:自定義增加軟件包支持
MySQL root user needs sudo login
Cases of OpenCV image enhancement
Screen display of charging pile design -- led driver ta6932
STM32 interrupt switch
Not many people can finally bring their interests to college graduation
Dynamic layout management
Windows下MySQL的安装和删除
Basic knowledge of communication interface
Development of intelligent charging pile (I): overview of the overall design of the system