class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> res;
if (nums.size() == 0)
{
return res;
}
deque<int> dq;
for (int i = 0;i < k; i++)
{
while (!dq.empty() && nums[dq.back()] < nums[i])
{
dq.pop_back();
}
dq.push_back(i);
}
res.push_back(nums[dq.front()]);
for (int i = k; i < nums.size(); i++)
{
if (i - k == dq.front())
{
dq.pop_front();
}
while (!dq.empty() && nums[dq.back()] < nums[i])
{
dq.pop_back();
}
dq.push_back(i);
res.push_back(nums[dq.front()]);
}
return res;
}
};