当前位置:网站首页>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); */
边栏推荐
- Label Semantic Aware Pre-training for Few-shot Text Classification
- getopt_ Typical use of long function
- Opencv gray histogram, histogram specification
- Positive and negative sample division and architecture understanding in image classification and target detection
- Leetcode-513: find the lower left corner value of the tree
- Leetcode-112: path sum
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- 波士顿房价预测(TensorFlow2.9实践)
- Drive and control program of Dianchuan charging board for charging pile design
猜你喜欢
4.1 Temporal Differential of one step
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
[C question set] of Ⅵ
2.2 DP: Value Iteration & Gambler‘s Problem
Deep learning by Pytorch
Vgg16 migration learning source code
Leetcode 300 最长上升子序列
Opencv gray histogram, histogram specification
Leetcode-513:找树的左下角值
随机推荐
『快速入门electron』之实现窗口拖拽
Discrete-event system
2.2 DP: Value Iteration & Gambler‘s Problem
Leetcode-106:根据中后序遍历序列构造二叉树
About windows and layout
pycharm 无法引入自定义包
20220604数学:x的平方根
Problems encountered when MySQL saves CSV files
Pycharm cannot import custom package
CV learning notes - reasoning and training
Installation and removal of MySQL under Windows
20220606数学:分数到小数
Opencv interview guide
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Leetcode-100:相同的树
LeetCode - 705 设计哈希集合(设计)
getopt_ Typical use of long function
El table X-axis direction (horizontal) scroll bar slides to the right by default
Leetcode-112: path sum
The data read by pandas is saved to the MySQL database