239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。
例如,
给定 nums = [1,3,-1,-3,5,3,6,7],和 k = 3 。
窗口位置 最大值
--------------- -----
[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
由此可见,返回最大的滑动窗口为:[3,3,5,5,6,7]。
注意:
你可以假设 k 一直都是有效的,例如:1 ≤ k ≤ 输入数组的大小,输入数组不为空。
进阶:
你能在线性时间复杂度内解决此题吗?
详见:https://leetcode.com/problems/sliding-window-maximum/description/
Java实现:
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int n=nums.length;
if(n==0||nums==null){
return new int[0];
}
int[] res=new int[n-k+1];
for(int i=0;i<n-k+1;++i){
int maxVal=nums[i];
for(int j=i;j<i+k;++j){
if(nums[j]>maxVal){
maxVal=nums[j];
}
}
res[i]=maxVal;
}
return res;
}
}
C++实现:
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n=nums.size();
vector<int> res;
if(n==0||nums.empty())
{
return res;
}
for(int i=0;i<n-k+1;++i)
{
int maxVal=nums[i];
for(int j=i;j<i+k;++j)
{
if(nums[j]>maxVal)
{
maxVal=nums[j];
}
}
res.push_back(maxVal);
}
return res;
}
};
239 Sliding Window Maximum 滑动窗口最大值的更多相关文章
- [leetcode]239. Sliding Window Maximum滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- [LeetCode] Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- [LeetCode] Sliding Window Median 滑动窗口中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- POJ - 2823 Sliding Window (滑动窗口入门)
An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...
- Sliding Window(滑动窗口)
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 58002 Accepted: 16616 Case Time Limi ...
随机推荐
- CI设置表单验证规则
CodeIgniter 允许你为单个表单域创建多个验证规则,按顺序层叠在一起, 你也可以同时对表单域的数据进行预处理.要设置验证规则, 可以使用 set_rules() 方法: $this->f ...
- csu1364 Interview
对拍了一波才找到的错误,此题我用的是二分答案加倍增查询,实际上query那里我觉得仍然有缺陷,因为每一次我的查找还是在循环找到一个k使得x+2^k <= y,而错的地方也正在此地,一开始没有判断 ...
- [bzoj3238][Ahoi2013]差异_后缀数组_单调栈
差异 bzoj-3238 Ahoi-2013 题目大意:求任意两个后缀之间的$LCP$的和. 注释:$1\le length \le 5\cdot 10^5$. 想法: 两个后缀之间的$LCP$和显然 ...
- UVALive7042(博弈论)
题意: Bob和Alice在有向图内玩游戏,n个顶点,m条边. 每人一颗棋子,初始位置分别是x,y. Bob先手,轮流操作,每次只能走一条有向边. 结束条件: 1.不能操作的人输 2.两个棋子重合Bo ...
- Ubuntu 16.04安装WinRAR/7-Zip(基于CrossOver)
基于CrossOver的WinRAR/7-Zip有如下缺点: 1.不能像Windows那样右键菜单解压 可以解决的问题: 1.可以使用提供的浏览工具进行文件选择再解压,只是在操作上多一步. 2.类似百 ...
- Kettle循环删除数据
1.问题描述: 某个系统原库的数据同步到备份库.但是由于原库的的数据会物理删除,此时需要删除备份库的数据. 2.不理想的解决1: 1)首先从备份库获取该表的所有ID: 2)循环备份库的ID,去原库检测 ...
- 原想着mysql里放些文件什么的,查完资料还是算了
三种东西永远不要放到数据库里 1.图片,文件,二进制数据,文件还是放入文件服务器吧或者分布式文件系统 2.短生命期数据 3.日志文件 mysql中一张表的数据是全部在一个数据文件中的.如果大字段的数据 ...
- GNS3配置SecureCRT
C:\SecureCRT\SecureCRT.exe /script D:\GNS3\DyRouter.vbs /T /telnet 127.0.0.1 %p "D:\Program Fil ...
- C# 如何修改Form不能修改窗体大小
把窗体的FormBorderSytle改一下就可以了,改成FixedSingle或者Fixed3D都可以
- Oracle中长度为0字符串与null等价
不试不知道,Oracle中,长度为0的字符串'' 居然与null等价! 众所周知,null代表空,什么都不存在,而一个字符串'',虽然长度为0,但毕竟已经是一个字符串,二者怎么能等价,混为一谈呢. 在 ...