<p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>

<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>

<ul>
	<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
	<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>

<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], minK, maxK &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        # let's iterate the array until find good array
        # while mainatiniong array good. increment left part 
        # traverse left until bad value
        # traverse right until bad value
        # set n to the right bad value
        
        # iterate right until find a good array or bad value
        # when found add all th next arrays until bad value or the end
        # move left ptr and continue to add values
        # move to the next good array
        r = 0
        l = 0
        
        max_count = 0
        min_count = 0
        # queue [num of good, end of array]
        def bad_value(i):
            return nums[i] < minK or nums[i] > maxK
        
        def reset_good():
            nonlocal max_count, min_count
            max_count = 0
            min_count = 0
        buffer = []
        ans = 0
        def process_q(end_of_good):
            nonlocal ans, buffer
            for num_of_arr, end_idx in buffer:
                ans += num_of_arr * (end_of_good - end_idx)
            
            buffer = []
        
        def adj_min_max(i, sign):
            nonlocal max_count, min_count
            if nums[i] == maxK:
                max_count += 1 * sign

            if nums[i] == minK:
                min_count += 1 * sign
        
        def good_sub_arr():
            return min_count > 0 and max_count > 0
        # [1,3,5,2,7,5], minK = 1, maxK = 5
        # r = 0, [1, 0]
        # r = 1, [1, 0]
        # r = 2, [1, 1], [(1, 2)]
        # r = 3, 
        # r = 4, [0, 0], ans = 1 * (4 - 2) = 2
        while r < len(nums):
            if bad_value(r):
                process_q(r)
                reset_good()
                l = r+1
            
            adj_min_max(r, 1)
            num_arrays = 0

            while good_sub_arr():
                num_arrays += 1
                adj_min_max(l, -1)
                l+= 1

            if num_arrays:
                buffer.append([num_arrays, r])
            r += 1
        
        process_q(len(nums))

        return ans

844