<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:
    #define pp pair<int,int> 
    // Push into back stack while maintaining the max in the back stack
    void enq(stack<pp> &front, stack<pp> &back, int value, int &currmax) {
        if(back.empty()){
            back.push({value, value});
            currmax = value;
            return;
        }

        currmax = back.top().second;
        
        back.push({value, max(currmax, value)});
        currmax = max(currmax, value);
    }

    // Pop from front stack. If front is empty, move elements from back to front
    void deq(stack<pp> &front, stack<pp> &back, int &currmax) {
        if (front.empty()) {
            int maxVal = INT_MIN;
            while (!back.empty()) {
                auto ele = back.top();
                back.pop();
                // Recalculate max while pushing into the front stack
                front.push({ele.first, max(maxVal, ele.first)});
                maxVal = max(maxVal, ele.first);
            }
        }
        front.pop();  // Remove the front element
        currmax = getmax(front, back);
    }

    // Get the maximum by comparing the top max values from both stacks
    int getmax(stack<pp> &a, stack<pp> &b) {
        int maxVal = INT_MIN;
        if (!a.empty()) maxVal = max(maxVal, a.top().second);
        if (!b.empty()) maxVal = max(maxVal, b.top().second);
        return maxVal;
    }

    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        if (k == 1) return nums;  // If window size is 1, every element is its own max

        int n = nums.size();
        stack<pp> back, front;
        int currmax = INT_MIN;

        // Initialize the first window
        for (int i = 0; i < k; i++) {
            enq(front, back, nums[i], currmax);
        }

        vector<int> ans;
        ans.push_back(getmax(front, back));  // First window's max
        int backmax;


        // Slide the window through the rest of the array
        for (int i = k; i < n; i++) {
            deq(front, back, currmax);  // Remove the element that's sliding out of the window
            enq(front, back, nums[i], currmax);  // Add the new element
            ans.push_back(getmax(front, back));  // Get the max of the current window
        }

        return ans;
    }
};

156051