当前位置:网站首页>Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
2022-07-03 10:12:00 【Cute at the age of three @d】




List + Small cap pile + Stack ( Stack of double ended queue implementation )




import java.util.*;
class DinnerPlates {
//list Stack deque
// A queue that holds all subscripts that are not full
private PriorityQueue<Integer> deque;
// Store all stacks in order
private List<Deque<Integer>> list;
// Stack capacity
private int capacity;
public DinnerPlates(int capacity) {
this.capacity = capacity;
// Subscripts are sorted from small to large
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 there is currently a stack that is not full
if(deque.size() > 0){
// Take out the first stack that is not full from left to right
Deque<Integer> stack = list.get(deque.peek());
// hold val Put in stack
stack.offerFirst(val);
// If the stack is full
// Pop the stack from the never full stack queue
if(stack.size() == this.capacity)
deque.poll();
}
else{
// The current stack is full
// Reopen a stack
Deque<Integer> stack = new LinkedList<>();
// hold val Put in stack
stack.offerFirst(val);
// Put the stack in the stack queue
list.add(stack);
// If the stack is not full
if(stack.size() < this.capacity){
// Add the stack to the under stack queue
deque.offer(list.size()-1);
}
}
}
public int pop() {
// If there is no stack
if(list.size() == 0)
return -1;
int val = -1;
int index = list.size()-1;
// take list Delete the empty stack at the end
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); */
边栏推荐
- Basic use and actual combat sharing of crash tool
- Retinaface: single stage dense face localization in the wild
- LeetCode - 900. RLE 迭代器
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- yocto 技术分享第四期:自定义增加软件包支持
- LeetCode - 919. Full binary tree inserter (array)
- Connect Alibaba cloud servers in the form of key pairs
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- 使用sed替换文件夹下文件
- Swing transformer details-1
猜你喜欢

CV learning notes - image filter

The underlying principle of vector

yocto 技术分享第四期:自定义增加软件包支持

Opencv feature extraction sift
![[C question set] of Ⅵ](/img/49/eb31cd26f7efbc4d57f17dc1321092.jpg)
[C question set] of Ⅵ

Retinaface: single stage dense face localization in the wild

Adaptiveavgpool1d internal implementation

MySQL root user needs sudo login

CV learning notes - edge extraction

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
随机推荐
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
yocto 技术分享第四期:自定义增加软件包支持
Matplotlib drawing
Pycharm cannot import custom package
About windows and layout
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Leetcode-112: path sum
Discrete-event system
CV learning notes ransca & image similarity comparison hash
LeetCode - 705 设计哈希集合(设计)
CV learning notes - image filter
Dynamic layout management
getopt_ Typical use of long function
(2) New methods in the interface
20220602数学:Excel表列序号
Leetcode-112:路径总和
Opencv notes 20 PCA
MySQL root user needs sudo login
Swing transformer details-1
CV learning notes - clustering