当前位置:网站首页>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.
边栏推荐
猜你喜欢
[微服务]多级缓存
ECCV 2022|通往数据高效的Transformer目标检测器
OpenCV perspective transform
The components of the basis of An animation movie clip animation between traditional filling
An工具介绍之骨骼工具
Basic principle of the bulk of the animation and shape the An animation tip point
An introduction to the camera
图像融合GAN-FM学习笔记
365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯
[Practical skills] APP video tutorial for updating APP in CANFD, I2C, SPI and serial port mode of single-chip bootloader (2022-08-01)
随机推荐
shell编程之条件语句
Sogou news-数据集
The Yangtze river commercial Banks to the interview
An工具介绍之形状工具及渐变变形工具
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
技术分享 | 接口自动化测试如何搞定 json 响应断言?
PyTorch builds a classification network model (Mnist dataset, fully connected neural network)
An introduction to the camera
An工具介绍之3D工具
Golang GMP 原理
self-discipline
图像融合GAN-FM学习笔记
Notepad++ install jsonview plugin
An基本工具介绍之选择线条工具(包教会)
使用工作队列管理器(三)
shell编程条件语句
软件测试面试(四)
【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
Use %Status value
Sogou news - dataset