<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:
        # TLE Issue
        if k == 1:
            return max(nums)
        total = 0
        n = len(nums)
        i = 0
        j = 1
        s = set([nums[0]])
        cur_sum = nums[0]

        while j < len(nums):
            
            if nums[j] not in s:
                cur_sum = cur_sum + nums[j]
                if j - i == k - 1:
                    total = max(total, cur_sum)
                    s.remove(nums[i])
                    s.add(nums[j])
                    cur_sum = cur_sum - nums[i]
                    i = i + 1
                    j = j + 1
                
                else:
                    s.add(nums[j])
                    j = j + 1
            
            else:
                s.remove(nums[i])
                cur_sum = cur_sum - nums[i]
                i = i + 1
                # print(nums[i], nums[j], cur_sum)
                if i == j:
                    cur_sum = nums[i]
                    s = set([nums[i]])
                    j = j + 1
                print(i, j)
        return total

        # Use dictionary to record the frequency. 
        # If length of dictionary equals to k, meaning each item appear only once!!

        # d = {}
        # i = 0
        # j = 0
        # answer = 0
        # cur_sum = 0

        # while j < len(nums):
        #     cur_sum = cur_sum + nums[j]

        #     if nums[j] in d:
        #         d[nums[j]] = d[nums[j]] + 1
        #     else:
        #         d[nums[j]] = 1
            
        #     if j - i == k - 1:
        #         if len(d) == k:
        #             answer = max(answer, cur_sum)
        #             # This step need to be optimized. Don't sum nums[i:j+1]. Instead record every sum in iteration

        #         d[nums[i]] = d[nums[i]] - 1
        #         cur_sum = cur_sum - nums[i]
                
        #         if d[nums[i]] == 0:
        #             del d[nums[i]]

        #         i = i + 1
            
        #     j = j + 1
            

        # return answer



            
            

                
                


677