<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>

<p>We call an index <code>i</code> in the range <code>k &lt;= i &lt; n - k</code> <strong>good</strong> if the following conditions are satisfied:</p>

<ul>
	<li>The <code>k</code> elements that are just <strong>before</strong> the index <code>i</code> are in <strong>non-increasing</strong> order.</li>
	<li>The <code>k</code> elements that are just <strong>after</strong> the index <code>i</code> are in <strong>non-decreasing</strong> order.</li>
</ul>

<p>Return <em>an array of all good indices sorted in <strong>increasing</strong> order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,1,1,3,4,1], k = 2
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong> There are two good indices in the array:
- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,1,2], k = 2
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no good indices in this array.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>

from collections import deque
class Solution:
    def goodIndices(self, nums: List[int], k: int) -> List[int]:
        n = len(nums)
        ret = []
        prefix, suffix = [1 for _ in range(n)], [1 for _ in range(n)]
        for i in range(1,n):
            if nums[i] <= nums[i-1]:
                prefix[i] = prefix[i-1]+1

        for i in range(2, n):
            if nums[-i] <= nums[-i+1]:
                suffix[-i] = suffix[-i+1] + 1
        for i in range(1,n-1):
            if prefix[i-1] >= k and suffix[i+1] >= k:
                ret.append(i)
        return ret

        preStack = deque([x for x in nums[:k]])
        postStack = deque([x for x in nums[k+1:2*k+1]])
        n, i = len(nums), k
        ret = []
        final = False
        while i < (n-k):
            if final: break
            # validate preStack
            prev, skips = preStack[0], 0
            for j in range(1, k):
                if prev < preStack[j]:
                    skips = j
                    break
                prev = preStack[j]

            prev, skips2 = postStack[0], 0
            for j in range(1, k):
                if prev > postStack[j]:
                    skips2 = j
                    break
                prev = postStack[j]

            if skips or skips2:
                if max(skips, skips) + i + k + 1 >= n:
                    final = True
                    break
                preStack = preStack[max(skips, skips2):] + nums[i:i+max(skips, skips2)]
                postStack = postStack[max(skips, skips2):] + nums[i+k+1:i+k+max(skips, skips2)]
                i += max(skips, skips2)
                continue
            ret.append(i)
            preStack.popleft()
            preStack.append(nums[i])
            postStack.popleft()
            if (i+k+1) >= n: break
            postStack.append(nums[i+k+1])
            i += 1
        return ret
710