<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 node:
    def __init__(self, val = None, prev = None, next = None, index = None):
        self.val = val
        self.prev = prev
        self.next = next
        self.index = index

class deque:
    def __init__(self):
        self.head = node()
        self.tail = node()
        self.head.next = self.tail
        self.tail.prev = self.head
        self.size = 0
    
    def append(self, node):
        node.prev = self.tail.prev
        node.next = self.tail
        self.tail.prev.next = node
        self.tail.prev = node  
        self.size += 1
    
    def remove(self, node):
        p, n = node.prev, node.next
        p.next = n
        n.prev = p
        self.size -= 1


class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        Q = deque()
        for i in range(k):
            n = node(nums[i], index = i)
            end = Q.tail.prev
            while (Q.size > 0 and end.val <= nums[i]):
                prev = end.prev
                Q.remove(end)
                end = prev
            Q.append(n)
        res = [Q.head.next.val]

        for i in range(k, len(nums)):
            # remove old
            if (Q.head.next.index == i - k):
                Q.remove(Q.head.next)
            end = Q.tail.prev
            while (Q.size > 0 and end.val <= nums[i]):
                prev = end.prev
                Q.remove(end)
                end = prev
            # add new
            n = node(nums[i], index = i)
            Q.append(n)
            res.append(Q.head.next.val)
        return res



34783