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

/*
# brute force: for every N, we do k comparisons, time O(N*k).k ~ N, so we could have O(N^2), at N~10^5, time would explode.
# maintain a sorted array / heap. insertion O(logN), deletion O(logN). How do we delete a num in heap and re-heapify?
# use hash to save location of val. on val replacement: 
# heap: how to re-heapify with new val?

Use BST: every time we locate the old val, delete it if count == 0, and insert new val / increase count.
procedure: 1. set up std::map: {val : count}. init map with first k values. for any subsequent insertion, locate old value, decrease count / remove it, and then insert new node / increase count.
*/


class Solution {
public:
    void printMap(map<int, int> m) {
        cout << "[ ";
        for(auto const& i : m) {
            cout << "(" << i.first << ", " << i.second << ") ";
        }
        cout << "]" << endl;
    }

    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> ret(nums.size() - k + 1, 0); // init return vector
        map<int, int> bst;
        // init
        for(size_t i = 0; i < k; ++i) {
            int num = nums[i];
            auto it = bst.find(num);
            if(it != bst.end()) {
                it->second ++;
            }
            else {
                bst.insert(make_pair(num, 1));
            }
        }
        size_t j = 0;
        ret[j] = bst.rbegin()->first; // set last in bst to be max
        j++;

        for(size_t i = k; i < nums.size(); ++i, ++j) {
            size_t i_remove = i - k;
            int num_remove = nums[i_remove];
            int num = nums[i];
            if(num_remove == num) { // same number, no change
                ret[j] = ret[j-1];
                continue;
            }
            auto it_remove = bst.find(num_remove);
            if(it_remove->second > 1) {
                it_remove->second--;
            }
            else{
                bst.erase(it_remove);
            }
            auto it_add = bst.find(num);
            if(it_add != bst.end()) {
                it_add->second++;
            }
            else {
                bst.insert(make_pair(num, 1));
            }
            ret[j] = bst.rbegin()->first; // set last in bst to be max

            //cout << "i = " << i << ", j = " << j << ", max: " << ret[j] << ", bst: ";
            //printMap(bst);
        }

        return ret;
    }
};
174623