<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the max sliding window</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong> 
Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       <strong>3</strong>
 1 [3  -1  -3] 5  3  6  7       <strong>3</strong>
 1  3 [-1  -3  5] 3  6  7      <strong> 5</strong>
 1  3  -1 [-3  5  3] 6  7       <strong>5</strong>
 1  3  -1  -3 [5  3  6] 7       <strong>6</strong>
 1  3  -1  -3  5 [3  6  7]      <strong>7</strong>
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        if(nums.empty() || k == 0) {
            return res;
        }
        
        deque<int> work;
        
        auto push_num = [&work, nums](int i) {
            while(work.size() && (nums[i] > nums[work.back()])) {
                //cout << "pop: " << work.back() << endl;
                work.pop_back();
            }
            work.push_back( i );
            //cout << "push: " << work.back() << endl;
        };
        
        //build the first window
        int r = 0;
        for(; (r < nums.size()) && (r < k); ++r) {
            push_num( r );
        }
        // r may be out of range if k == size
        // r is out of window, we have not pushed
        // nums[r] into work
        
        // record the first windows
        assert( work.size() );
        res.push_back( nums[work.front()] );
        
        int l = 0;
        // move window and record max
        while(r < nums.size()) {
            // start with r pointing at next element out of the window
            ++l; // [l:r] is now the new legal window
            //cout << "l: " << l << " r: " << r << endl;
            // add new value
            push_num( r );
            // find the (potentially new) max
            // discard all that is left of l
            // note that may be l == r
            while(work.size() && (work.front() < l)) {
                //cout << "pop " << work.front() << "<" << l << endl;
                work.pop_front();
            }
            //cout << "work.size: " << work.size() << endl;
            // new max is leftmost in work
            assert( work.size() );
            res.push_back( nums[work.front()] );
            ++r;
        }
        // r is out of range of nums, no more windows
        
        return res;
    }
};
141848