当前位置:网站首页>剑指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 reasons for MySQL slow query

MySQL事务(transaction) (有这篇就足够了..)

On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels

限流的原理

剑指offer专项突击版 ---- 第 6 天

Unity Framework Design Series: How Unity Designs Network Frameworks

MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细

DVWA之SQL注入

mysql stored procedure

ES source code API call link source code analysis
随机推荐
Pytorch教程Introduction中的神经网络实现示例
目标检测学习笔记
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
sql statement - how to query data in another table based on the data in one table
面试官,不要再问我三次握手和四次挥手
CentOS7 —— yum安装mysql
Paginate the list collection and display the data on the page
MySQL optimization slow log query
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
CentOS7 - yum install mysql
MySQL8--Windows下使用压缩包安装的方法
Anaconda配置环境指令
工作流编排引擎-Temporal
centos7安装mysql5.7
12个MySQL慢查询的原因分析
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
MySQL (updating)
Numpy中np.meshgrid的简单用法示例
mysql uses on duplicate key update to update data in batches
mysql使用on duplicate key update批量更新数据