<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the maximum subarray sum of all the subarrays of <code>nums</code> that meet the following conditions:</p>

<ul>
	<li>The length of the subarray is <code>k</code>, and</li>
	<li>All the elements of the subarray are <strong>distinct</strong>.</li>
</ul>

<p>Return <em>the maximum subarray sum of all the subarrays that meet the conditions</em><em>.</em> If no subarray meets the conditions, return <code>0</code>.</p>

<p><em>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</em></p>

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

<pre>
<strong>Input:</strong> nums = [1,5,4,2,9,9,9], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,4,4], k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    
    def maximumSubarraySum(self, nums: List[int], k: int) -> int:
        
        #create a sliding window for the subarray and init it first
        #calculate the windows sum

        #slide the windows forward by 1
        #Check if the elements are distinct
        #calculate the new window sum
        #take the MAX of maxSUM and the windowSUM
        #at the end return the maxSUM

        #We check if distinct by keeping a dict/map of elements in the window
        #map will keep track of occurances/count of each element encountered
        #When initializing the window, create a map with count of each element up to kth elements in the List
        #add to count if an element encountered when window moves
        #delete from count if window moves forward

        windowSum = 0
        maxSUM = 0
        window = []
        seen = collections.Counter(nums[:k])
        print(seen)

        for x in range(k):
            windowSum = windowSum + nums[x]            

        #Check the first window
        #if the map has same amount of elements as our window size, then its unique
        #so we can update the maxSUM, otherwise it remains as 0
        if len(seen) == k:
            maxSUM = windowSum

        # print(windowSum)

        #iterate thru the array by sliding the window
        #Move the window forward by subtracting the current element and adding the next element to get the new windowSum
        #Remove int from map if moving forward
        #Add new int to map
        for i in range(len(nums)-k):
            windowSum = windowSum - nums[i] + nums[i+k]   
            seen[nums[i]] -= 1
            seen[nums[i+k]] += 1

            #clean and get rid of elements that have 0 occurances
            if seen[nums[i]] == 0:
                del seen[nums[i]]
            
            #if all of occurances in map equal to window size then elements have only appeared once, therefore unique
            if(len(seen) == k):
                maxSUM = max(windowSum, maxSUM)
        # print(seen)
        # print(maxSUM)
        return maxSUM

    
652