-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path2799-count-complete-subarrays-in-an-array.js
42 lines (38 loc) · 1.17 KB
/
2799-count-complete-subarrays-in-an-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 2799. Count Complete Subarrays in an Array
* https://leetcode.com/problems/count-complete-subarrays-in-an-array/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var countCompleteSubarrays = function(nums) {
const totalDistinct = new Set(nums).size;
const frequency = new Map();
let completeCount = 0;
let left = 0;
for (let right = 0; right < nums.length; right++) {
frequency.set(nums[right], (frequency.get(nums[right]) || 0) + 1);
while (frequency.size === totalDistinct) {
completeCount += nums.length - right;
const leftNum = nums[left];
frequency.set(leftNum, frequency.get(leftNum) - 1);
if (frequency.get(leftNum) === 0) {
frequency.delete(leftNum);
}
left++;
}
}
return completeCount;
};