<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:
        # solution redone here, original solutions further below
        n = len(nums)
        prev_min, prev_max = None, None
        res = 0
        i, j = 0, 0
        while j < n:
            x = nums[j]
            if x > maxK or x < minK:
                j += 1
                i = j
                prev_min, prev_max = None, None
                continue
            if x == minK:
                prev_min = j
            if x == maxK:
                prev_max = j
            if prev_min is not None and prev_max is not None:
                res += min(prev_min, prev_max) - i + 1
            j += 1
        return res









































        # # let dp[j] be number of subarrays whose right endpoint index is at j
        # # let i be the smallest index <= j s.t. nums[i:j+1] has all elements between minK and maxK
        # # if minK <= nums[j+1] <= maxK, we know that the same i works for j+1
        # # else we know that dp[j+1] = 0, and we can increment i to j+2
        # # we just need to take care to find the most recent occurrences of min and max, because the most
        # # recent occurrences of minK and maxK must be included in the subarray that ends at j in order
        # # for it to be valid
        # # so we only track the subarrays whose left index is somewhere between i and the minimum index of
        # # the most recent occurrences of minK, maxK.

        # n = len(nums)
        # i, j, res = 0, 0, 0
        # found_min, found_max = False, False
        # most_recent_min, most_recent_max = -1, -1

        # while j < n:
        #     if minK <= nums[j] <= maxK:
        #         if nums[j] == minK:
        #             found_min = True
        #             most_recent_min = j
        #         if nums[j] == maxK:
        #             found_max = True
        #             most_recent_max = j
        #         if found_min and found_max:
        #             res += min(most_recent_max, most_recent_min) - i + 1
        #     else:
        #         found_min = False
        #         found_max = False
        #         i = j + 1
        #     j += 1
        # return res











































        # # # left_bound is the index such that the element at this index has
        # # # an element that is either greater than maxK or less than minK
        # # # in particular, any valid subarray whose ending index is larger than
        # # # to left_bound, must also have starting index larger than left_bound
        # # res, left_bound = 0, -1
        # # most_recent_min, most_recent_max = -1, -1
        # # for i in range(len(nums)):
        # #     if nums[i] == minK:
        # #         most_recent_min = i
        # #     if nums[i] == maxK:
        # #         most_recent_max = i
        # #     if nums[i] < minK:
        # #         most_recent_min, most_recent_max = -1, -1
        # #         left_bound = i
        # #     if nums[i] > maxK:
        # #         most_recent_min, most_recent_max = -1, -1
        # #         left_bound = i
        # #     if most_recent_min != -1 and most_recent_max != -1:
        # #         # add in number of valid subarrays ending at index i
        # #         if left_bound == -1:
        # #             curr = min(most_recent_min, most_recent_max) + 1
        # #         else:
        # #             curr = min(most_recent_min, most_recent_max) - left_bound
        # #         res += curr
        # # return res
351