<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:
        # prev = 0
        # prev_idx = -1
        # first_in_range = -1
        # cur_range = None
        # tot = 0
        # for i in range(len(nums)):
        #     if nums[i] == minK:
        #         if prev == 1:
        #             cur_range = (prev_idx, i)
        #         prev = -1
        #         prev_idx = i
            
        #     if nums[i] == maxK:
        #         prev = 1
        #         prev_idx = i

        #     if nums[i] > maxK or nums[i] < minK:
        #         prev = 0
        #         prev_idx = -1
        #         first_in_range = -1
        #         cur_range = None
        cont_regs = []
        cur_region = []
        for n in nums:
            if n < minK or n > maxK:
                if len(cur_region) > 0:
                    cont_regs.append(cur_region)
                cur_region = []
            else:
                cur_region.append(n)
        if len(cur_region) > 0:
            cont_regs.append(cur_region)
        print(cont_regs)
        tot = 0
        for cr in cont_regs:
            reg_start = None
            regs = []
            prev = 0
            for i in range(len(cr)):
                # print(reg_start, prev, cr[i])
                if cr[i] == minK:
                    if reg_start != None and prev == 1:
                        regs.append((reg_start, i))
                        reg_start = i
                        prev = -1
                    else:
                        reg_start = i
                        prev = -1
                if cr[i] == maxK:
                    if reg_start != None and prev == -1:
                        regs.append((reg_start, i))
                        reg_start = i
                        prev = 1
                    else:
                        reg_start = i
                        prev = 1
            print(regs)
            if len(regs) == 0:
                continue
            prev = 0
            for reg in regs:
                tot += (reg[0] - prev + 1) * (len(cr) - reg[1])
                prev = reg[0]+1
        return tot

                
        
817