#2799
Count Complete Subarrays in an Array
pupil · 480 · lc medium +27 · verified · 75.9% accepted · 1,130 likes · top 87%
Description
An array nums of positive integers is given.
A subarray of nums is complete when it contains the same number of distinct elements as the entire array.
Return the total number of complete subarrays.
A subarray is a contiguous non-empty part of an array.
Example 1:
Input: nums = [1,3,1,2,2]
Output: 4
Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].
Example 2:
Input: nums = [5,5,5,5]
Output: 10
Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.
Code
1
2
3