class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> index;
vector<int> maxValue;
if(nums.size() < k || k <=0)
return maxValue;
for(int i = 0;i<k;i++)
{
while(index.size() > 0 && nums[index.back()] < nums[i])
index.pop_back();
index.push_back(i);
}
for(int i = k;i<nums.size();i++)
{
maxValue.push_back(nums[index.front()]);
while(index.size() > 0 && nums[index.back()] < nums[i])
index.pop_back();
if(index.size() > 0 && index.front() <= i - k)
index.pop_front();
index.push_back(i);
}
//if(k == nums.size())
maxValue.push_back(nums[index.front()]);
return maxValue;
}
};
LeetCode || Sliding Window Maximum
最新推荐文章于 2023-07-24 04:42:51 发布