<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 MaxQueue {
public:
    MaxQueue() {

    }
    
    int max_v() {
        if (!push_stack.empty() &&  !pop_stack.empty()) {
            return max(push_stack.top().second, pop_stack.top().second);
        } else if (!push_stack.empty()) {
            return push_stack.top().second;
        } else if (!pop_stack.empty()) {
            return pop_stack.top().second;
        }

        return 0;
    }

    void push(int val) {
        int max_value = val;
        if (!push_stack.empty()) max_value = max(max_value, push_stack.top().second);
        push_stack.push({val, max_value});
    }

    void pop() {
        int max_value = std::numeric_limits<int>::min();
        if (pop_stack.empty()) {
            while(!push_stack.empty()) {
                pair<int, int> top = push_stack.top();
                max_value = max(top.first, max_value);
                pop_stack.push({top.first, max_value});
                push_stack.pop();
            }
        }
        pop_stack.pop();
    }
private:
    stack<pair<int, int>> push_stack;
    stack<pair<int, int>> pop_stack;
};

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> ans;
        MaxQueue q;
        for (int i = 0; i < nums.size(); ++i) {
            q.push(nums[i]);
            if (i>=k) q.pop();
            if (i >= k-1) ans.push_back(q.max_v());
        }
        return ans;
        
    }
};
156051