<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>

"""
___
Goal:
___

return # of fixed-bound subarrays

___
Examples:
___

Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output: 2


Input: nums = [1,1,1,1], minK = 1, maxK = 1
Output: 10


___
Assumptions:
___

1. lower bound looks like O(len(nums)) largest subarray being nums

2. minK: minimum value in subarray is this

3. maxK: max value in subarray is this

4. 2 <= len(nums) <= 10^5

5. upper bound: looks like a O(len(nums)log(len(nums))) solution


___
Algorithms:
___


Algo 1: Brute Force via sorting 
T: O(len(nums)^2*log(len(nums)))
S: O(len(nums))


Algo 2: Expandable/Contracting Sliding Window with min/max heaps
T: O(len(nums)log(len(nums)))
S: O(len(nums))
- Discovered at 8:30

___
PsuedoCode:
___

scan through nums:
    each iteration:
        - right pointer hits minK <= n <= maxK 
            - if min_heap and max_heap and min_heap[0] == minK and max_heap[0] == maxK
                - num_subarrays += 1
        - otherwise:
            - empty both heaps

psuedocode done at 12:37
"""


# class Solution:
#     def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        
#         min_heap = []
#         max_heap = []
#         num_fixed = 0

#         for n in nums:
            
#             if minK <= n <= maxK:
#                 heappush(min_heap, n)
#                 heappush(max_heap, -n)

#                 if min_heap and max_heap and min_heap[0] == minK and -max_heap[0] == maxK:
#                     num_fixed += 1
#             else:
#                 min_heap = []
#                 max_heap = []

#         return num_fixed

# done coding at 16:23

class Solution:
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        minI, maxI = -1, -1
        
        left = -1
        count = 0
        
        for right in range(len(nums)):
            
            if not (minK <= nums[right] <= maxK):
                minI = right
                maxI = right
                left = right
            
            if nums[right] == minK:
                minI = right
            if nums[right] == maxK:
                maxI = right

            count += min(minI, maxI) - left
        
        return count

548