Skip to content

Commit 3c1f76c

Browse files
authored
feat: update solution to lc problem: No.2799 (#4370)
1 parent 0def1c6 commit 3c1f76c

File tree

4 files changed

+117
-68
lines changed

4 files changed

+117
-68
lines changed

solution/2700-2799/2799.Count Complete Subarrays in an Array/README.md

+33-22
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,28 @@ function countCompleteSubarrays(nums: number[]): number {
186186

187187
```rust
188188
use std::collections::HashSet;
189+
189190
impl Solution {
190191
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
191-
let mut set: HashSet<&i32> = nums.iter().collect();
192+
let mut s = HashSet::new();
193+
for &x in &nums {
194+
s.insert(x);
195+
}
196+
let cnt = s.len();
192197
let n = nums.len();
193-
let m = set.len();
194198
let mut ans = 0;
199+
195200
for i in 0..n {
196-
set.clear();
201+
s.clear();
197202
for j in i..n {
198-
set.insert(&nums[j]);
199-
if set.len() == m {
200-
ans += n - j;
201-
break;
203+
s.insert(nums[j]);
204+
if s.len() == cnt {
205+
ans += 1;
202206
}
203207
}
204208
}
205-
ans as i32
209+
210+
ans
206211
}
207212
}
208213
```
@@ -358,27 +363,33 @@ function countCompleteSubarrays(nums: number[]): number {
358363

359364
```rust
360365
use std::collections::HashMap;
361-
use std::collections::HashSet;
366+
362367
impl Solution {
363368
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
364-
let n = nums.len();
365-
let m = nums.iter().collect::<HashSet<&i32>>().len();
366-
let mut map = HashMap::new();
369+
let mut d = HashMap::new();
370+
for &x in &nums {
371+
d.insert(x, 1);
372+
}
373+
let cnt = d.len();
367374
let mut ans = 0;
368-
let mut i = 0;
369-
for j in 0..n {
370-
*map.entry(nums[j]).or_insert(0) += 1;
371-
while map.len() == m {
372-
ans += n - j;
373-
let v = map.entry(nums[i]).or_default();
374-
*v -= 1;
375-
if *v == 0 {
376-
map.remove(&nums[i]);
375+
let n = nums.len();
376+
d.clear();
377+
378+
let (mut i, mut j) = (0, 0);
379+
while j < n {
380+
*d.entry(nums[j]).or_insert(0) += 1;
381+
while d.len() == cnt {
382+
ans += (n - j) as i32;
383+
let e = d.get_mut(&nums[i]).unwrap();
384+
*e -= 1;
385+
if *e == 0 {
386+
d.remove(&nums[i]);
377387
}
378388
i += 1;
379389
}
390+
j += 1;
380391
}
381-
ans as i32
392+
ans
382393
}
383394
}
384395
```

solution/2700-2799/2799.Count Complete Subarrays in an Array/README_EN.md

+51-24
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Hash Table + Enumeration
67+
68+
First, we use a hash table to count the number of distinct elements in the array, denoted as $cnt$.
69+
70+
Next, we enumerate the left endpoint index $i$ of the subarray and maintain a set $s$ to store the elements in the subarray. Each time we move the right endpoint index $j$ to the right, we add $nums[j]$ to the set $s$ and check whether the size of the set $s$ equals $cnt$. If it equals $cnt$, it means the current subarray is a complete subarray, and we increment the answer by $1$.
71+
72+
After the enumeration ends, we return the answer.
73+
74+
Time complexity: $O(n^2)$, Space complexity: $O(n)$, where $n$ is the length of the array.
6775

6876
<!-- tabs:start -->
6977

@@ -178,23 +186,28 @@ function countCompleteSubarrays(nums: number[]): number {
178186

179187
```rust
180188
use std::collections::HashSet;
189+
181190
impl Solution {
182191
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
183-
let mut set: HashSet<&i32> = nums.iter().collect();
192+
let mut s = HashSet::new();
193+
for &x in &nums {
194+
s.insert(x);
195+
}
196+
let cnt = s.len();
184197
let n = nums.len();
185-
let m = set.len();
186198
let mut ans = 0;
199+
187200
for i in 0..n {
188-
set.clear();
201+
s.clear();
189202
for j in i..n {
190-
set.insert(&nums[j]);
191-
if set.len() == m {
192-
ans += n - j;
193-
break;
203+
s.insert(nums[j]);
204+
if s.len() == cnt {
205+
ans += 1;
194206
}
195207
}
196208
}
197-
ans as i32
209+
210+
ans
198211
}
199212
}
200213
```
@@ -205,7 +218,15 @@ impl Solution {
205218

206219
<!-- solution:start -->
207220

208-
### Solution 2
221+
### Solution 2: Hash Table + Two Pointers
222+
223+
Similar to Solution 1, we can use a hash table to count the number of distinct elements in the array, denoted as $cnt$.
224+
225+
Next, we use two pointers to maintain a sliding window, where the right endpoint index is $j$ and the left endpoint index is $i$.
226+
227+
Each time we fix the left endpoint index $i$, we move the right endpoint index $j$ to the right. When the number of distinct elements in the sliding window equals $cnt$, it means that all subarrays from the left endpoint index $i$ to the right endpoint index $j$ and beyond are complete subarrays. We then increment the answer by $n - j$, where $n$ is the length of the array. Afterward, we move the left endpoint index $i$ one step to the right and repeat the process.
228+
229+
Time complexity: $O(n)$, Space complexity: $O(n)$, where $n$ is the length of the array.
209230

210231
<!-- tabs:start -->
211232

@@ -342,27 +363,33 @@ function countCompleteSubarrays(nums: number[]): number {
342363

343364
```rust
344365
use std::collections::HashMap;
345-
use std::collections::HashSet;
366+
346367
impl Solution {
347368
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
348-
let n = nums.len();
349-
let m = nums.iter().collect::<HashSet<&i32>>().len();
350-
let mut map = HashMap::new();
369+
let mut d = HashMap::new();
370+
for &x in &nums {
371+
d.insert(x, 1);
372+
}
373+
let cnt = d.len();
351374
let mut ans = 0;
352-
let mut i = 0;
353-
for j in 0..n {
354-
*map.entry(nums[j]).or_insert(0) += 1;
355-
while map.len() == m {
356-
ans += n - j;
357-
let v = map.entry(nums[i]).or_default();
358-
*v -= 1;
359-
if *v == 0 {
360-
map.remove(&nums[i]);
375+
let n = nums.len();
376+
d.clear();
377+
378+
let (mut i, mut j) = (0, 0);
379+
while j < n {
380+
*d.entry(nums[j]).or_insert(0) += 1;
381+
while d.len() == cnt {
382+
ans += (n - j) as i32;
383+
let e = d.get_mut(&nums[i]).unwrap();
384+
*e -= 1;
385+
if *e == 0 {
386+
d.remove(&nums[i]);
361387
}
362388
i += 1;
363389
}
390+
j += 1;
364391
}
365-
ans as i32
392+
ans
366393
}
367394
}
368395
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
use std::collections::HashSet;
2+
23
impl Solution {
34
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
4-
let mut set: HashSet<&i32> = nums.iter().collect();
5+
let mut s = HashSet::new();
6+
for &x in &nums {
7+
s.insert(x);
8+
}
9+
let cnt = s.len();
510
let n = nums.len();
6-
let m = set.len();
711
let mut ans = 0;
12+
813
for i in 0..n {
9-
set.clear();
14+
s.clear();
1015
for j in i..n {
11-
set.insert(&nums[j]);
12-
if set.len() == m {
13-
ans += n - j;
14-
break;
16+
s.insert(nums[j]);
17+
if s.len() == cnt {
18+
ans += 1;
1519
}
1620
}
1721
}
18-
ans as i32
22+
23+
ans
1924
}
2025
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
use std::collections::HashMap;
2-
use std::collections::HashSet;
2+
33
impl Solution {
44
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();
810
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]);
1823
}
1924
i += 1;
2025
}
26+
j += 1;
2127
}
22-
ans as i32
28+
ans
2329
}
2430
}

0 commit comments

Comments
 (0)