当前位置:网站首页>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
- After clicking the Save button, you can only click it once
- Opencv+dlib to change the face of Mona Lisa
- 20220603 Mathematics: pow (x, n)
- MySQL root user needs sudo login
- Pycharm cannot import custom package
- LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
- CV learning notes alexnet
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
- Implementation of "quick start electronic" window dragging
猜你喜欢

3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation

LeetCode - 673. Number of longest increasing subsequences

YOLO_ V1 summary

Leetcode bit operation

Installation and removal of MySQL under Windows

CV learning notes - image filter

Swing transformer details-1

CV learning notes - deep learning

CV learning notes - clustering

03 fastjason solves circular references
随机推荐
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Octave instructions
On the problem of reference assignment to reference
Leetcode 300 longest ascending subsequence
Yocto technology sharing phase IV: customize and add software package support
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
2021-11-11 standard thread library
LeetCode - 933 最近的请求次数
Deep Reinforcement learning with PyTorch
Wireshark use
pycharm 无法引入自定义包
After clicking the Save button, you can only click it once
2.1 Dynamic programming and case study: Jack‘s car rental
Leetcode-112:路径总和
CV learning notes convolutional neural network
LeetCode - 5 最长回文子串
Leetcode-100: same tree
Flutter 退出当前操作二次确认怎么做才更优雅?
CV learning notes - image filter
Leetcode 300 最长上升子序列