当前位置:网站首页>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); */
边栏推荐
猜你喜欢

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

Pycharm cannot import custom package

CV learning notes ransca & image similarity comparison hash

LeetCode - 919. 完全二叉树插入器 (数组)

One click generate traffic password (exaggerated advertisement title)

Connect Alibaba cloud servers in the form of key pairs

Leetcode-513: find the lower left corner value of the tree

2.2 DP: Value Iteration & Gambler‘s Problem

Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:

Cases of OpenCV image enhancement
随机推荐
Deep learning by Pytorch
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
After clicking the Save button, you can only click it once
Opencv histogram equalization
Circular queue related design and implementation reference 1
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
RESNET code details
Implementation of "quick start electronic" window dragging
Cases of OpenCV image enhancement
2. Elment UI date selector formatting problem
03 fastjason solves circular references
. DLL and Differences between lib files
CV learning notes - camera model (Euclidean transformation and affine transformation)
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
ADS simulation design of class AB RF power amplifier
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)