File tree Expand file tree Collapse file tree 1 file changed +20
-14
lines changed
solution/2700-2799/2799.Count Complete Subarrays in an Array Expand file tree Collapse file tree 1 file changed +20
-14
lines changed Original file line number Diff line number Diff line change 1
1
use std:: collections:: HashMap ;
2
- use std :: collections :: HashSet ;
2
+
3
3
impl Solution {
4
4
pub fn count_complete_subarrays ( nums : Vec < i32 > ) -> i32 {
5
- let n = nums. len ( ) ;
6
- let m = nums. iter ( ) . collect :: < HashSet < & i32 > > ( ) . len ( ) ;
7
- let mut map = HashMap :: new ( ) ;
5
+ let mut d = HashMap :: new ( ) ;
6
+ for & x in & nums {
7
+ d. insert ( x, 1 ) ;
8
+ }
9
+ let cnt = d. len ( ) ;
8
10
let mut ans = 0 ;
9
- let mut i = 0 ;
10
- for j in 0 ..n {
11
- * map. entry ( nums[ j] ) . or_insert ( 0 ) += 1 ;
12
- while map. len ( ) == m {
13
- ans += n - j;
14
- let v = map. entry ( nums[ i] ) . or_default ( ) ;
15
- * v -= 1 ;
16
- if * v == 0 {
17
- map. remove ( & nums[ i] ) ;
11
+ let n = nums. len ( ) ;
12
+ d. clear ( ) ;
13
+
14
+ let ( mut i, mut j) = ( 0 , 0 ) ;
15
+ while j < n {
16
+ * d. entry ( nums[ j] ) . or_insert ( 0 ) += 1 ;
17
+ while d. len ( ) == cnt {
18
+ ans += ( n - j) as i32 ;
19
+ let e = d. get_mut ( & nums[ i] ) . unwrap ( ) ;
20
+ * e -= 1 ;
21
+ if * e == 0 {
22
+ d. remove ( & nums[ i] ) ;
18
23
}
19
24
i += 1 ;
20
25
}
26
+ j += 1 ;
21
27
}
22
- ans as i32
28
+ ans
23
29
}
24
30
}
You can’t perform that action at this time.
0 commit comments