aboutsummaryrefslogtreecommitdiff
path: root/count_complete_subarr_arr.py
diff options
context:
space:
mode:
Diffstat (limited to 'count_complete_subarr_arr.py')
-rw-r--r--count_complete_subarr_arr.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/count_complete_subarr_arr.py b/count_complete_subarr_arr.py
new file mode 100644
index 0000000..ca0371d
--- /dev/null
+++ b/count_complete_subarr_arr.py
@@ -0,0 +1,35 @@
+# 2799. Count Complete Subarrays in an Array
+from collections import Counter
+
+"""
+you are given an array 'nums' consisting of positive integers. we call a
+subarray of an array complete if the following condition is satisfied. the
+number of distinct elements in the subarray is equal to the number of
+distinct elements in the whole array. return the number of complete
+subarrays. a subarray is a contiguous non-empty part of an array.
+"""
+
+
+class Solution(object):
+ def countCompleteSubarrays(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ n, k, i = len(nums), len(set(nums)), 0
+ ans, cnt = 0, Counter()
+ for j in range(n):
+ cnt[nums[j]] += 1
+ while len(cnt) == k:
+ cnt[nums[i]] -= 1
+ if cnt[nums[i]] == 0:
+ del cnt[nums[i]]
+ i += 1
+ ans += i
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countCompleteSubarrays(nums=[1, 3, 1, 2, 2]))
+ print(obj.countCompleteSubarrays(nums=[5, 5, 5, 5]))