当前位置:网站首页>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); */
边栏推荐
- Retinaface: single stage dense face localization in the wild
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- (2) New methods in the interface
- 01 business structure of imitation station B project
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
- QT is a method of batch modifying the style of a certain type of control after naming the control
- is_ power_ of_ 2 judge whether it is a multiple of 2
- openCV+dlib实现给蒙娜丽莎换脸
- Circular queue related design and implementation reference 1
- 20220607其他:两整数之和
猜你喜欢
CV learning notes convolutional neural network
Basic use and actual combat sharing of crash tool
Discrete-event system
【C 题集】of Ⅵ
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
El table X-axis direction (horizontal) scroll bar slides to the right by default
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
yocto 技术分享第四期:自定义增加软件包支持
Octave instructions
随机推荐
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Connect Alibaba cloud servers in the form of key pairs
20220602 Mathematics: Excel table column serial number
LeetCode - 933 最近的请求次数
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Opencv gray histogram, histogram specification
CV learning notes - deep learning
Deep learning by Pytorch
LeetCode - 673. Number of longest increasing subsequences
Leetcode-106:根据中后序遍历序列构造二叉树
LeetCode - 673. 最长递增子序列的个数
About windows and layout
The data read by pandas is saved to the MySQL database
20220603数学:Pow(x,n)
2021-10-28
Retinaface: single stage dense face localization in the wild
The underlying principle of vector
Implementation of "quick start electronic" window dragging
Discrete-event system
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*