当前位置:网站首页>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); */
边栏推荐
- Google browser plug-in recommendation
- 20220607其他:两整数之和
- LeetCode - 715. Range 模块(TreeSet) *****
- 20220602 Mathematics: Excel table column serial number
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- Leetcode 300 longest ascending subsequence
- Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
- 波士顿房价预测(TensorFlow2.9实践)
- LeetCode - 673. 最长递增子序列的个数
- Problems encountered when MySQL saves CSV files
猜你喜欢

QT self drawing button with bubbles

CV learning notes - image filter

Opencv feature extraction sift

波士顿房价预测(TensorFlow2.9实践)

Opencv image rotation

Basic use and actual combat sharing of crash tool

Leetcode 300 最长上升子序列

Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*

LeetCode - 706 设计哈希映射(设计) *

CV learning notes ransca & image similarity comparison hash
随机推荐
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Opencv feature extraction sift
使用密钥对的形式连接阿里云服务器
Simulate mouse click
LeetCode - 715. Range 模块(TreeSet) *****
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
2021-11-11 standard thread library
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Matplotlib drawing
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
LeetCode - 706 设计哈希映射(设计) *
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
20220531数学:快乐数
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
LeetCode - 5 最长回文子串
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
『快速入门electron』之实现窗口拖拽
Leetcode - 933 number of recent requests
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Flutter 退出当前操作二次确认怎么做才更优雅?