当前位置:网站首页>Sword finger offer 59 - I. maximum value of sliding window
Sword finger offer 59 - I. maximum value of sliding window
2022-06-29 19:40:00 【Yake1965】
The finger of the sword Offer 59 - I. Maximum sliding window
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++){
// A monotonous queue
while (!q.isEmpty() && nums[i] >= nums[q.peekLast()]) {
q.pollLast();
}
q.add(i); // Indexes
int left = i - k + 1; // When left >= 0 Start sliding window when
if (q.peek() < left) q.poll();
if (left >= 0) res[left] = nums[q.peek()];
}
return res;
}
}
边栏推荐
- go: 如何编写一个正确的udp服务端
- 乐鑫面试流程
- Automatically obtain local connection and network address modification
- JVM(2) 垃圾回收
- 自动获取本地连接及网络地址修改
- How important is it to make a silver K-line chart?
- Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
- 数据安全解决方案的大时代
- [proteus simulation] matrix keyboard interrupt scanning
- The era of data security solutions
猜你喜欢

3-2主机发现-三层发现

3-3 host discovery - layer 4 discovery

How important is it to make a silver K-line chart?

数据基础设施升级窗口下,AI 新引擎的技术方法论

细说GaussDB(DWS)复杂多样的资源负载管理手段

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

Performance improvement at the cost of other components is not good

电脑ssd硬盘怎么安装使用

做白银k线图有多重要?

Flutter 调用百度地图APP实现位置搜索、路线规划
随机推荐
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
jfinal中如何使用过滤器监控Druid监听SQL执行?
剑指 Offer 59 - I. 滑动窗口的最大值
7. cancellation and closing
3-2主机发现-三层发现
There is no small green triangle on the method in idea
Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
[network orientation training] - Enterprise Park Network Design - [had done]
童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布
3-3主机发现-四层发现
深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
In 2022, the financial interest rate has dropped, so how to choose financial products?
创作者基金会 6 月份亮点
JVM(4) 字節碼技術+運行期優化
4-1端口扫描技术
物理验证LVS流程和技术点滴(上)
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
powershell命令仅输出目录列表
自动获取本地连接及网络地址修改
4-2端口Banner信息获取