当前位置:网站首页>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,先声明cur
This 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 to1
to get a new array,利用Arrays
种的sort
method 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.
边栏推荐
猜你喜欢
Sogou news - dataset
类和对象(中上)
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
Real number rounding and writing to file (C language file)
shell编程之条件语句
Graphic animation and button animation of an animation basic component
BOM系列之sessionStorage
An工具介绍之宽度工具、变形工具与套索工具
IDEA的模板(Templates)
基于php家具销售管理系统获取(php毕业设计)
随机推荐
如何让history历史记录前带时间戳
When Nodejs installation depends on cpnm, the install shows Error: Cannot find module 'fs/promises'
JS get browser type
GameFi 行业下滑但未出局| June Report
安防监控必备的基础知识「建议收藏」
Golang 数组和切片
Golang 字典 map
An animation optimization of traditional guide layer animation
【深度学习】高效轻量级语义分割综述
leetcode 11. 盛最多水的容器
Golang 结构体&方法
PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记
An工具介绍之摄像头
An动画优化之遮罩层动画
云计算服务主要安全风险及应对措施初探
来广州找工作有一个多月了,今天终于有着落了,工资7000
Golang 字符串
[Practical skills] APP video tutorial for updating APP in CANFD, I2C, SPI and serial port mode of single-chip bootloader (2022-08-01)
An introduction to 3D tools
类和对象(中上)