<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers.</p>

<p>We call a subarray of an array <strong>complete</strong> if the following condition is satisfied:</p>

<ul>
	<li>The number of <strong>distinct</strong> elements in the subarray is equal to the number of distinct elements in the whole array.</li>
</ul>

<p>Return <em>the number of <strong>complete</strong> subarrays</em>.</p>

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

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

<pre>
<strong>Input:</strong> nums = [1,3,1,2,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,5,5,5]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 2000</code></li>
</ul>

class Solution:
    def countCompleteSubarrays(self, nums: List[int]) -> int:
        s = set(nums)
        total_num = len(s)
        res = 0

        # for a fixed left_index, find the smallest right_index so that nums[left_index, right_index] is complete subarray
        # traverse all possible left_index
        # use binary search to find the smallest right_idx for each left_idx

        def isComplete(nums, left_idx, right_idx, s):
            subarr = nums[left_idx:right_idx+1]
            if len(set(subarr)) == s: 
                # print("reach 1")
                return True
            else: 
                # print("reach 2")
                return False
        
        for i in range(len(nums)):
            # print(i)
            if not isComplete(nums, i, len(nums)-1, total_num): break
            # print(i)
            left, right = i, len(nums)-1
            smallest_right_idx = len(nums) - 1
            while left <= right:
                print(left, right)
                mid = left + (right - left) // 2
                if left == right: 
                    smallest_right_idx = left
                    break
                elif isComplete(nums, i, mid, total_num):
                    right = mid
                else:
                    left = mid + 1
            # print(i, smallest_right_idx)
            res += (len(nums) - smallest_right_idx)
            # print(i, res)
        return res
138