From 0c8168e4adbdf16fe111f014d02fba1d6b253fbd Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 24 Apr 2025 06:23:26 +0800 Subject: [PATCH 1/4] feat: update solution to lc problem: No.2799 --- .../README.md | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/solution/2700-2799/2799.Count Complete Subarrays in an Array/README.md b/solution/2700-2799/2799.Count Complete Subarrays in an Array/README.md index b011c8621ffeb..8ed4fea0f389d 100644 --- a/solution/2700-2799/2799.Count Complete Subarrays in an Array/README.md +++ b/solution/2700-2799/2799.Count Complete Subarrays in an Array/README.md @@ -186,23 +186,28 @@ function countCompleteSubarrays(nums: number[]): number { ```rust use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let mut set: HashSet<&i32> = nums.iter().collect(); + let mut s = HashSet::new(); + for &x in &nums { + s.insert(x); + } + let cnt = s.len(); let n = nums.len(); - let m = set.len(); let mut ans = 0; + for i in 0..n { - set.clear(); + s.clear(); for j in i..n { - set.insert(&nums[j]); - if set.len() == m { - ans += n - j; - break; + s.insert(nums[j]); + if s.len() == cnt { + ans += 1; } } } - ans as i32 + + ans } } ``` @@ -358,27 +363,33 @@ function countCompleteSubarrays(nums: number[]): number { ```rust use std::collections::HashMap; -use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let n = nums.len(); - let m = nums.iter().collect::>().len(); - let mut map = HashMap::new(); + let mut d = HashMap::new(); + for &x in &nums { + d.insert(x, 1); + } + let cnt = d.len(); let mut ans = 0; - let mut i = 0; - for j in 0..n { - *map.entry(nums[j]).or_insert(0) += 1; - while map.len() == m { - ans += n - j; - let v = map.entry(nums[i]).or_default(); - *v -= 1; - if *v == 0 { - map.remove(&nums[i]); + let n = nums.len(); + d.clear(); + + let (mut i, mut j) = (0, 0); + while j < n { + *d.entry(nums[j]).or_insert(0) += 1; + while d.len() == cnt { + ans += (n - j) as i32; + let e = d.get_mut(&nums[i]).unwrap(); + *e -= 1; + if *e == 0 { + d.remove(&nums[i]); } i += 1; } + j += 1; } - ans as i32 + ans } } ``` From a43285c603034745aa3147f5e776bba62a385784 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 24 Apr 2025 06:27:19 +0800 Subject: [PATCH 2/4] Update README_EN.md --- .../README_EN.md | 75 +++++++++++++------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/solution/2700-2799/2799.Count Complete Subarrays in an Array/README_EN.md b/solution/2700-2799/2799.Count Complete Subarrays in an Array/README_EN.md index 989fc082af15e..ed86d2e591faa 100644 --- a/solution/2700-2799/2799.Count Complete Subarrays in an Array/README_EN.md +++ b/solution/2700-2799/2799.Count Complete Subarrays in an Array/README_EN.md @@ -63,7 +63,15 @@ tags: -### Solution 1 +### Solution 1: Hash Table + Enumeration + +First, we use a hash table to count the number of distinct elements in the array, denoted as $cnt$. + +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$. + +After the enumeration ends, we return the answer. + +Time complexity: $O(n^2)$, Space complexity: $O(n)$, where $n$ is the length of the array. @@ -178,23 +186,28 @@ function countCompleteSubarrays(nums: number[]): number { ```rust use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let mut set: HashSet<&i32> = nums.iter().collect(); + let mut s = HashSet::new(); + for &x in &nums { + s.insert(x); + } + let cnt = s.len(); let n = nums.len(); - let m = set.len(); let mut ans = 0; + for i in 0..n { - set.clear(); + s.clear(); for j in i..n { - set.insert(&nums[j]); - if set.len() == m { - ans += n - j; - break; + s.insert(nums[j]); + if s.len() == cnt { + ans += 1; } } } - ans as i32 + + ans } } ``` @@ -205,7 +218,15 @@ impl Solution { -### Solution 2 +### Solution 2: Hash Table + Two Pointers + +Similar to Solution 1, we can use a hash table to count the number of distinct elements in the array, denoted as $cnt$. + +Next, we use two pointers to maintain a sliding window, where the right endpoint index is $j$ and the left endpoint index is $i$. + +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. + +Time complexity: $O(n)$, Space complexity: $O(n)$, where $n$ is the length of the array. @@ -342,27 +363,33 @@ function countCompleteSubarrays(nums: number[]): number { ```rust use std::collections::HashMap; -use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let n = nums.len(); - let m = nums.iter().collect::>().len(); - let mut map = HashMap::new(); + let mut d = HashMap::new(); + for &x in &nums { + d.insert(x, 1); + } + let cnt = d.len(); let mut ans = 0; - let mut i = 0; - for j in 0..n { - *map.entry(nums[j]).or_insert(0) += 1; - while map.len() == m { - ans += n - j; - let v = map.entry(nums[i]).or_default(); - *v -= 1; - if *v == 0 { - map.remove(&nums[i]); + let n = nums.len(); + d.clear(); + + let (mut i, mut j) = (0, 0); + while j < n { + *d.entry(nums[j]).or_insert(0) += 1; + while d.len() == cnt { + ans += (n - j) as i32; + let e = d.get_mut(&nums[i]).unwrap(); + *e -= 1; + if *e == 0 { + d.remove(&nums[i]); } i += 1; } + j += 1; } - ans as i32 + ans } } ``` From 6024eddaa5549af9d86d9dedd5ae9c652d3e7c76 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 24 Apr 2025 06:27:51 +0800 Subject: [PATCH 3/4] Update Solution.rs --- .../Solution.rs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs b/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs index 1b67ee82dbef3..6c7281c121152 100644 --- a/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs +++ b/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs @@ -1,20 +1,25 @@ use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let mut set: HashSet<&i32> = nums.iter().collect(); + let mut s = HashSet::new(); + for &x in &nums { + s.insert(x); + } + let cnt = s.len(); let n = nums.len(); - let m = set.len(); let mut ans = 0; + for i in 0..n { - set.clear(); + s.clear(); for j in i..n { - set.insert(&nums[j]); - if set.len() == m { - ans += n - j; - break; + s.insert(nums[j]); + if s.len() == cnt { + ans += 1; } } } - ans as i32 + + ans } } From 2484073e3b248f33ed7c1aaec664ec84ab0ddbf9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 24 Apr 2025 06:28:19 +0800 Subject: [PATCH 4/4] Update Solution2.rs --- .../Solution2.rs | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution2.rs b/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution2.rs index f10d7ee65ab8f..01ce642a5214b 100644 --- a/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution2.rs +++ b/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution2.rs @@ -1,24 +1,30 @@ use std::collections::HashMap; -use std::collections::HashSet; + impl Solution { pub fn count_complete_subarrays(nums: Vec) -> i32 { - let n = nums.len(); - let m = nums.iter().collect::>().len(); - let mut map = HashMap::new(); + let mut d = HashMap::new(); + for &x in &nums { + d.insert(x, 1); + } + let cnt = d.len(); let mut ans = 0; - let mut i = 0; - for j in 0..n { - *map.entry(nums[j]).or_insert(0) += 1; - while map.len() == m { - ans += n - j; - let v = map.entry(nums[i]).or_default(); - *v -= 1; - if *v == 0 { - map.remove(&nums[i]); + let n = nums.len(); + d.clear(); + + let (mut i, mut j) = (0, 0); + while j < n { + *d.entry(nums[j]).or_insert(0) += 1; + while d.len() == cnt { + ans += (n - j) as i32; + let e = d.get_mut(&nums[i]).unwrap(); + *e -= 1; + if *e == 0 { + d.remove(&nums[i]); } i += 1; } + j += 1; } - ans as i32 + ans } }