File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,435 LeetCode solutions in JavaScript
1
+ # 1,436 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1408
1408
2726|[ Calculator with Method Chaining] ( ./solutions/2726-calculator-with-method-chaining.js ) |Easy|
1409
1409
2727|[ Is Object Empty] ( ./solutions/2727-is-object-empty.js ) |Easy|
1410
1410
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|
1411
1412
2818|[ Apply Operations to Maximize Score] ( ./solutions/2818-apply-operations-to-maximize-score.js ) |Hard|
1412
1413
2843|[ Count Symmetric Integers] ( ./solutions/2843-count-symmetric-integers.js ) |Easy|
1413
1414
2873|[ Maximum Value of an Ordered Triplet I] ( ./solutions/2873-maximum-value-of-an-ordered-triplet-i.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments