当前位置:网站首页>剑指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();
*/
边栏推荐
- 【一起学Rust】Rust的Hello Rust详细解析
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 运用flask框架发送短信验证码的流程及具体代码
- 再见了繁琐的Excel,掌握数据分析处理技术就靠它了
- Unity resources management series: Unity framework how to resource management
- Mysql应用安装后找不到my.ini文件
- 三次握手与四次挥手
- Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
- Reference code series_1. Hello World in various languages
- Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
猜你喜欢

Linux系统安装mysql(rpm方式安装)
![[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade](/img/af/7a2cdcc6535c04c508c9ddf9ee0cb2.png)
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade

Temporal客户端模型

CentOS7 - yum install mysql
![<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍](/img/a4/8c75fab6a9858c5ddec25f6a8300fb.png)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

matlab simulink欠驱动水面船舶航迹自抗扰控制研究
![[MQ I can speak for an hour]](/img/ef/863c994ac3a7de157bd39545218558.jpg)
[MQ I can speak for an hour]

MySQL8--Windows下使用压缩包安装的方法

Mysql应用安装后找不到my.ini文件
随机推荐
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
剑指offer专项突击版 --- 第 3 天
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
Refinement of the four major collection frameworks: Summary of List core knowledge
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
mysql uses on duplicate key update to update data in batches
Temporal对比Cadence
Input length must be multiple of 8 when decrypting with padded cipher
Flink sink redis 写入Redis
1. Get data - requests.get()
Redis的初识
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
基于web3.0使用钱包Metamask的三方登陆
MySQL8.0.26安装配置教程(windows 64位)
C语言的文件操作(一)
Lock wait timeout exceeded解决方案
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
MySQL-如何分库分表?一看就懂