<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 <algorithm>
#include <queue>
#include <unordered_map>

class MapHeap {
private:
    std::priority_queue<int> maxHeap; // Max heap to store values
    std::unordered_map<int, int> countMap; // Map to store counts of each value

public:
    // Add a value to the heap
    void add(int value) {
        maxHeap.push(value);
        countMap[value]++;
    }

    // Delete a value from the heap
    void deleteValue(int value) {
        if (countMap.find(value) != countMap.end()) {
            countMap[value]--;
            if (countMap[value] == 0) {
                countMap.erase(value);
            }
        }
    }

    // Peek the top value in the heap
    int peek() {
        // Clean up the top of the heap if it's not in the map
        while (!maxHeap.empty() && countMap[maxHeap.top()] == 0) {
            maxHeap.pop();
        }
        if (maxHeap.empty()) {
            throw std::runtime_error("Heap is empty.");
        }
        return maxHeap.top();
    }
};

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        std::vector<int> toReturn;
        if (nums.size() == 0) {
            return toReturn;
        }

        int numToReturn = nums.size() - k + 1;
        toReturn.reserve(numToReturn);

        MapHeap heap;
        int start_idx = 0;
        int end_idx = k - 1;
        for (int idx = start_idx; idx <= end_idx; ++idx) {
            heap.add(nums[idx]);
        }

        for (int retIdx = 0; retIdx < numToReturn; ++retIdx) {
            toReturn.push_back(heap.peek());
            if (retIdx < numToReturn - 1) {
                heap.deleteValue(nums[start_idx]);
                start_idx += 1;
                end_idx += 1;
                heap.add(nums[end_idx]);
            }
        }

        return toReturn;
    }
};
169161