当前位置:网站首页>剑指offer基础版 ---- 第27天
剑指offer基础版 ---- 第27天
2022-07-31 05:09:00 【米兰的小红黑】

class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0 || k == 0) return new int[0];
Deque<Integer> deque = new LinkedList<>();
int[] res = new int[nums.length - k + 1];
for(int j = 0, i = 1 - k; j < nums.length; i++, j++) {
// 删除 deque 中对应的 nums[i-1]
if(i > 0 && deque.peekFirst() == nums[i - 1])
deque.removeFirst();
// 保持 deque 递减
while(!deque.isEmpty() && deque.peekLast() < nums[j])
deque.removeLast();
deque.addLast(nums[j]);
// 记录窗口最大值
if(i >= 0)
res[i] = deque.peekFirst();
}
return res;
}
}

class MaxQueue {
int[] q = new int[20000];
int begin = 0, end = 0;
public MaxQueue() {
}
public int max_value() {
int ans = -1;
for (int i = begin; i != end; ++i) {
ans = Math.max(ans, q[i]);
}
return ans;
}
public void push_back(int value) {
q[end++] = value;
}
public int pop_front() {
if (begin == end) {
return -1;
}
return q[begin++];
}
}
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue obj = new MaxQueue();
* int param_1 = obj.max_value();
* obj.push_back(value);
* int param_3 = obj.pop_front();
*/
边栏推荐
- 质量小议12 -- 以测代评
- Mysql application cannot find my.ini file after installation
- MySQL optimization slow log query
- C语言的文件操作(一)
- Redis的初识
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- Pytorch教程Introduction中的神经网络实现示例
- Refinement of the four major collection frameworks: Summary of List core knowledge
- SQL语句中对时间字段进行区间查询
- Typec手机有线网卡网线转网口转接口快充方案
猜你喜欢

分布式事务处理方案大 PK!

TOGAF之架构标准规范(一)

如何将项目部署到服务器上(全套教程)

Lock wait timeout exceeded解决方案

账号或密码多次输入错误,进行账号封禁

Linux系统安装mysql(rpm方式安装)

matlab abel变换图片处理

Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

Refinement of the four major collection frameworks: Summary of List core knowledge
随机推荐
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Linux系统安装mysql(rpm方式安装)
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
110 MySQL interview questions and answers (continuously updated)
Reference code series_1. Hello World in various languages
基于web3.0使用钱包Metamask的三方登陆
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Pytorch教程Introduction中的神经网络实现示例
SQL statement to range query time field
【一起学Rust】Rust学习前准备——注释和格式化输出
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Unity mobile game performance optimization series: performance tuning for the CPU side
目标检测学习笔记
【MQ我可以讲一个小时】
MySQL-Explain详解
MySQL优化:从十几秒优化到三百毫秒
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
Temporal线上部署
[mysql improves query efficiency] Mysql database query is slow to solve the problem
如何将项目部署到服务器上(全套教程)