当前位置:网站首页>剑指 Offer 59 - I. 滑动窗口的最大值
剑指 Offer 59 - I. 滑动窗口的最大值
2022-06-29 19:37:00 【Yake1965】
剑指 Offer 59 - I. 滑动窗口的最大值
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
if(n == 0) return new int[0];
int[] ans = new int[n - k + 1];
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> b[0] - a[0]);
for(int i = 0; i < k; i++){
q.offer(new int[]{
nums[i], i});
}
ans[0] = q.peek()[0];
for(int i = k; i < n; i++){
q.offer(new int[]{
nums[i], i});
while(q.peek()[1] <= i - k) q.poll();
ans[i-k+1] = q.peek()[0];
}
return ans;
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
if(n == 0) return new int[0];
PriorityQueue<Integer> q = new PriorityQueue<>((i, j) -> nums[j] - nums[i]);
int[] ans = new int[n - k + 1];
for(int i = 0; i < k; i++){
q.offer(i);
}
ans[0] = nums[q.peek()];
for(int i = k; i < n; i++){
q.offer(i);
while(q.peek() <= i - k){
q.poll();
}
ans[i-k+1] = nums[q.peek()];
}
return ans;
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
if(n == 0) return new int[0];
int[] res = new int[n - k + 1];
Deque<Integer> q = new LinkedList<>();
for(int i = 0; i < n; i++){
// 单调队列
while (!q.isEmpty() && nums[i] >= nums[q.peekLast()]) {
q.pollLast();
}
q.add(i); // 索引
int left = i - k + 1; // 当 left >= 0 时开始滑动窗口
if (q.peek() < left) q.poll();
if (left >= 0) res[left] = nums[q.peek()];
}
return res;
}
}
边栏推荐
- QC protocol + Huawei fcp+ Samsung AFC fast charging 5v9v chip fs2601 application
- 【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)
- Common knowledge of ECS security settings
- KDD 2022 | 协同过滤中考虑表征对齐和均匀性
- What about frequent network disconnection of win11 system? Solution to win11 network instability
- NLP 类问题建模方案探索实践
- 【软件测试】01 -- 软件生命周期、软件开发模型
- KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
- freeswitch拨打分机号
- Connaissance générale des paramètres de sécurité du serveur Cloud
猜你喜欢

Common knowledge of ECS security settings

以其他组件为代价的性能提升不是好提升

How is the combination of convolution and transformer optimal?

QC protocol + Huawei fcp+ Samsung AFC fast charging 5v9v chip fs2601 application

Exploration and practice of NLP problem modeling scheme

mysql远程连接

Where is the win11 installation permission set? Win11 installation permission setting method
![[笔记]再笔记--边干边学Verilog HDL –008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[笔记]再笔记--边干边学Verilog HDL –008

k线图经典图解(收藏版)

@SneakyThrows注解
随机推荐
Why is informatization ≠ digitalization? Finally someone made it clear
2022年理财利率都降了,那该如何选择理财产品?
细说GaussDB(DWS)复杂多样的资源负载管理手段
MBA-day19 如果p则q矛盾关系p 且非q
freeswitch拨打分机号
云服务器的安全设置常识
乐鑫面试流程
Connaissance générale des paramètres de sécurité du serveur Cloud
微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条
Canonical的工程师们正努力解决Firefox Snap的性能问题
3 - 3 découverte de l'hôte - découverte à quatre niveaux
Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
7. cancellation and closing
3-3主机发现-四层发现
虎符限币种提现 用户曲线出金即亏损
After CDN is added to the website, the Font Icon reports an error access control allow origin
誰在抖音文玩裏趁亂打劫?
谁在抖音文玩里趁乱打劫?
mysql远程连接