当前位置:网站首页>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); */
边栏推荐
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Adaptiveavgpool1d internal implementation
- Pycharm cannot import custom package
- Dictionary tree prefix tree trie
- 4G module board level control interface designed by charging pile
- 使用sed替换文件夹下文件
- Circular queue related design and implementation reference 1
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
猜你喜欢

Blue Bridge Cup for migrant workers majoring in electronic information engineering

新系列单片机还延续了STM32产品家族的低电压和节能两大优势

Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program

LeetCode - 673. Number of longest increasing subsequences

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

Stm32f407 key interrupt

Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*

Dictionary tree prefix tree trie

Opencv histogram equalization

My notes on intelligent charging pile development (II): overview of system hardware circuit design
随机推荐
使用sed替换文件夹下文件
RESNET code details
An executable binary file contains more than machine instructions
Toolbutton property settings
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Positive and negative sample division and architecture understanding in image classification and target detection
Qcombox style settings
Stm32f04 clock configuration
There is no specific definition of embedded system
Leetcode - 933 number of recent requests
Swing transformer details-1
2021-11-11 standard thread library
Do you understand automatic packing and unpacking? What is the principle?
YOLO_ V1 summary
2. Elment UI date selector formatting problem
4G module board level control interface designed by charging pile
Simulate mouse click
QT is a method of batch modifying the style of a certain type of control after naming the control
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)