Skip to main content

2799. Count Complete Subarrays in an Array

link

For each occurrence, we just keep 1 time

When we are at i index and the counter == set number, it means we have all element for nums[left: i]

we can just add left+1 times

(nums[left-1: i], nums[left-2: i], nums[left-3: i], ... nums[0: i])

class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:

n = len(nums)

if n == 1:
return 1

S = set(nums)

M = defaultdict(int)

res = 0
l = 0

for i in range(n):
M[nums[i]] += 1

while l < i and M[nums[l]] > 1:
M[nums[l]] -= 1
l += 1
if len(M.keys()) == len(S):
res += l + 1

return res