<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>

#include <span>
#include <format>

class Compare {
public:
  bool operator()(const pair<int, size_t> &a, const pair<int, size_t> &b) {
    return a.first < b.first;
  }
};

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        auto N = nums.size();
        auto nums_span = span(nums);
        auto initial = nums_span.subspan(0, k);
        auto remaining = nums_span.subspan(k, N-k);

        priority_queue<std::pair<int, size_t>, vector<std::pair<int, size_t>>, Compare> data;
        vector<int> result;

    // Fill first result
    size_t index = 0;
    for (const auto &num : initial) {
    //   cout << std::format("Inserting: {}, Index: {}", num, index) << endl;
      data.push({num, index++});
    }

    result.push_back(data.top().first);

    // Remaining
    for (const auto &num : remaining) {
    //   cout << std::format("Inserting: {}, Index: {}", num, index) << endl;
      data.push({num, index});
      while (true) {
        auto top = data.top();
        // cout << std::format("Top: {}, Index: {}, ind: {}", top.first, top.second, index) << endl;
        if (top.second <= (index - k)) {
        //   cout << std::format("Erasing {}", top.first);
          data.pop();
        } else {
          result.push_back(top.first);
          break;
        }
      }
      index++;
    }

    return result;
  }
};
168068