Skip to content

Commit 5e6a219

Browse files
committed
Add solution #2799
1 parent 1ddec91 commit 5e6a219

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,435 LeetCode solutions in JavaScript
1+
# 1,436 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1408,6 +1408,7 @@
14081408
2726|[Calculator with Method Chaining](./solutions/2726-calculator-with-method-chaining.js)|Easy|
14091409
2727|[Is Object Empty](./solutions/2727-is-object-empty.js)|Easy|
14101410
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
1411+
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
14111412
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
14121413
2843|[Count Symmetric Integers](./solutions/2843-count-symmetric-integers.js)|Easy|
14131414
2873|[Maximum Value of an Ordered Triplet I](./solutions/2873-maximum-value-of-an-ordered-triplet-i.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2799. Count Complete Subarrays in an Array
3+
* https://leetcode.com/problems/count-complete-subarrays-in-an-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array nums consisting of positive integers.
7+
*
8+
* We call a subarray of an array complete if the following condition is satisfied:
9+
* - The number of distinct elements in the subarray is equal to the number of distinct
10+
* elements in the whole array.
11+
*
12+
* Return the number of complete subarrays.
13+
*
14+
* A subarray is a contiguous non-empty part of an array.
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
* @return {number}
20+
*/
21+
var countCompleteSubarrays = function(nums) {
22+
const totalDistinct = new Set(nums).size;
23+
const frequency = new Map();
24+
let completeCount = 0;
25+
let left = 0;
26+
27+
for (let right = 0; right < nums.length; right++) {
28+
frequency.set(nums[right], (frequency.get(nums[right]) || 0) + 1);
29+
30+
while (frequency.size === totalDistinct) {
31+
completeCount += nums.length - right;
32+
const leftNum = nums[left];
33+
frequency.set(leftNum, frequency.get(leftNum) - 1);
34+
if (frequency.get(leftNum) === 0) {
35+
frequency.delete(leftNum);
36+
}
37+
left++;
38+
}
39+
}
40+
41+
return completeCount;
42+
};

0 commit comments

Comments
 (0)