当前位置:网站首页>剑指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();
*/
边栏推荐
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
- On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- mysql使用on duplicate key update批量更新数据
- Flink sink ES 写入 ES(带密码)
- Flask 的初识
- Mysql application cannot find my.ini file after installation
- Kubernetes加入集群的TOKEN值过期
- SQL语句中对时间字段进行区间查询
- MYSQL下载及安装完整教程
猜你喜欢

为什么要用Flink,怎么入门使用Flink?

DVWA shooting range environment construction

MySQL优化之慢日志查询

MYSQL下载及安装完整教程

Why use Flink and how to get started with Flink?

Minesweeper game (written in c language)

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

Mysql——字符串函数

Mysql应用安装后找不到my.ini文件

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
随机推荐
Centos7 install mysql5.7
基于web3.0使用钱包Metamask的三方登陆
分布式事务处理方案大 PK!
Multiple table query of sql statement
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
C语言如何分辨大小端
mysql存储过程
MySQL (updating)
【MQ我可以讲一个小时】
matlab abel变换图片处理
Redis的初识
DVWA之SQL注入
1. Get data - requests.get()
如何将项目部署到服务器上(全套教程)
Unity resources management series: Unity framework how to resource management
12 reasons for MySQL slow query
快速掌握并发编程 --- 基础篇
Typec手机有线网卡网线转网口转接口快充方案
C语言教程(二)-printf及c自带的数据类型