当前位置:网站首页>剑指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();
*/
边栏推荐
- sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
- 【MQ我可以讲一个小时】
- docker安装postgresSQL和设置自定义数据目录
- 关于LocalDateTime的全局返回时间带“T“的时间格式处理
- Mysql应用安装后找不到my.ini文件
- Centos7 install mysql5.7
- Reference code series_1. Hello World in various languages
- 剑指offer专项突击版 ---- 第 6 天
- Distributed transaction processing solution big PK!
- Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
猜你喜欢
![2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。](/img/7f/130a9b733855a2bab07d26ffda2c49.png)
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。

The interviewer asked me TCP three handshake and four wave, I really

MySQL优化:从十几秒优化到三百毫秒

MySQL8.0.26安装配置教程(windows 64位)

SQL row-column conversion

pycharm专业版使用

Unity mobile game performance optimization series: performance tuning for the CPU side

Simple read operation of EasyExcel

工作流编排引擎-Temporal

账号或密码多次输入错误,进行账号封禁
随机推荐
CentOS7 - yum install mysql
DVWA之SQL注入
基于flask的三方登陆的流程
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
MySQL transaction isolation level, rounding
DVWA shooting range environment construction
可点击也可直接复制指定内容js
精解四大集合框架:List 核心知识总结
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
Apache DButils使用注意事项--with modifiers “public“
Centos7 install mysql5.7
Numpy中np.meshgrid的简单用法示例
Refinement of the four major collection frameworks: Summary of List core knowledge
SQL statement to range query time field
mysql5.7.35安装配置教程【超级详细安装教程】
剑指offer专项突击版 ---第 5 天
The monitoring of Doris study notes
MySQL常见面试题汇总(建议收藏!!!)
关于LocalDateTime的全局返回时间带“T“的时间格式处理