当前位置:网站首页>Sword finger offer 59 - ii Maximum value of the queue
Sword finger offer 59 - ii Maximum value of the queue
2022-06-29 19:40:00 【Yake1965】
The finger of the sword Offer 59 - II. The maximum value of the queue
class MaxQueue {
Queue<Integer> q;
Deque<Integer> dq;
public MaxQueue() {
this.q = new LinkedList<>();
this.dq = new LinkedList<>();
}
public int max_value() {
return this.dq.isEmpty() ? -1 : this.dq.peek();
}
public void push_back(int value) {
this.q.add(value);
// A monotonous queue Non strictly decreasing , Keep equal copies .
while(!this.dq.isEmpty() && value > this.dq.peekLast()){
dq.pollLast();
}
this.dq.add(value); // Additive elements
}
public int pop_front() {
if(this.q.isEmpty()) return -1;
int x = this.q.poll();
if(x == this.dq.peek()) this.dq.pop(); // Judgment by value
return x;
}
}
// Array simulation
class MaxQueue {
int[] q = new int[10000];
int[] dq = new int[10000];
int i = 0, j = 0, left = 0, top = 0;
public MaxQueue() {
}
public int max_value() {
return i == j ? -1 : this.dq[left];
}
public void push_back(int value) {
q[j++] = value;
// A monotonous queue Non strictly decreasing , Keep equal copies .
while(left <= top && value > dq[top]){
top --;
}
this.dq[++top] = value;
}
public int pop_front() {
if(i == j) return -1;
int x = q[i++];
if(x == dq[left]) left++; // Judgment by value
return x;
}
}
边栏推荐
- NLP - GIZA++ 实现词对齐
- 【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~
- JVM (4) Bytecode Technology + Runtime Optimization
- 洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
- Performance improvement at the cost of other components is not good
- [proteus simulation] matrix keyboard interrupt scanning
- Point, line, surface and body of enterprise digital transformation!
- Understanding of software test logic coverage
- 剑指 Offer 41. 数据流中的中位数
- idea中方法上没有小绿色三角
猜你喜欢

Understanding of software test logic coverage

3-3 host discovery - layer 4 discovery

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?

From CIO to Consultant: the transformation of it leaders

细说GaussDB(DWS)复杂多样的资源负载管理手段

【笔记】再笔记--边干边学Verilog HDL – 014

Exploration and practice of NLP problem modeling scheme

Game Maker 基金会呈献:归属之谷

创作者基金会 6 月份亮点
随机推荐
Tiger painter mengxiangshun's digital collection is on sale in limited quantities and comes with Maotai in the year of the tiger
static静态成员变量使用@Value注入方式
From CIO to Consultant: the transformation of it leaders
全局变量和静态变量的初始化
WPS和Excele
细说GaussDB(DWS)复杂多样的资源负载管理手段
4-2 port banner information acquisition
Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
一个mysql里有3306端口下,一个mysql有20多个数据库,怎么一键备份20多个数据库,做系统备份,防止数据误删除?
做白银k线图有多重要?
Where is the win11 installation permission set? Win11 installation permission setting method
【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...
3-3 host discovery - layer 4 discovery
技术保证质量,软件测试的这些测试方法你都掌握了吗?
Nacos problem
powershell命令仅输出目录列表
[software testing] 01 -- software life cycle and software development model
@SneakyThrows注解
Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
数据安全解决方案的大时代