当前位置:网站首页>The maximum number of sliding window
The maximum number of sliding window
2022-08-03 13:16:00 【Array_new】
滑动窗口的最大值
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值.
示例:
输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
提示:
你可以假设 k 总是有效的,在输入数组不为空的情况下,1 ≤ k ≤ 输入数组的大小.
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0)return nums;
int[] a=new int[k];
int[] end=new int[nums.length-k+1];
int j=0;
int cur=0;
for(int i=0;i<nums.length;i++){
a[j]=nums[i];
j++;
if(j==k){
Arrays.sort(a);
end[cur]=a[k-1];
i=cur;
cur++;
j=0;
}
if(i==nums.length+1)break;
}
return end;
}
}
This problem can be solved using arrays and pointers,先声明curThis pointer points to the first value of the sliding window due to looping+is executed after the loop so subtract it from the position pointed to1to get a new array,利用Arrays种的sortmethod to find the maximum value,So you can use simpleforLoop to find the answer,stored in the final array,It can also be solved by using a queue or stack to reduce the time complexity.
边栏推荐
猜你喜欢
随机推荐
An introduction to 3D tools
基于php家具销售管理系统获取(php毕业设计)
PyTorch构建神经网络预测气温(数据集对比,CPU与GPU对比)
力扣刷题 每日两题(一)
可视化图表设计Cookbook
An animation optimization of shape tween and optimization of traditional tweening
An introduction to the skeleton tool
Tinymce plugins [Tinymce扩展插件集合]
基于php校园医院门诊管理系统获取(php毕业设计)
15. PARTITIONS「建议收藏」
GameFi 行业下滑但未出局| June Report
leetcode16 Sum of the closest three numbers (sort + double pointer)
Five, the function calls
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
图像融合GAN-FM学习笔记
An动画基础之元件的影片剪辑效果
期货公司开户关注的关键点
d作者:d的新特性
shell编程之条件语句
使用工作队列管理器(三)









![[微服务]多级缓存](/img/58/72e01c789a862c058cba58b9113272.png)