From 9217542d132aad13094f1f7aaf8b522c5ed49711 Mon Sep 17 00:00:00 2001 From: YangFong Date: Sat, 26 Aug 2023 18:03:54 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2798~2800 - 2798.Number of Employees Who Met the Target - 2799.Count Complete Subarrays in an Array - 2800.Shortest String That Contains Three Strings --- .../README.md | 16 ++++ .../README_EN.md | 16 ++++ .../Solution.rs | 11 +++ .../README.md | 52 +++++++++++ .../README_EN.md | 52 +++++++++++ .../Solution.rs | 24 +++++ .../README.md | 87 +++++++++++++++++++ .../README_EN.md | 87 +++++++++++++++++++ .../Solution.rs | 38 ++++++++ 9 files changed, 383 insertions(+) create mode 100644 solution/2700-2799/2798.Number of Employees Who Met the Target/Solution.rs create mode 100644 solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs create mode 100644 solution/2800-2899/2800.Shortest String That Contains Three Strings/Solution.rs diff --git a/solution/2700-2799/2798.Number of Employees Who Met the Target/README.md b/solution/2700-2799/2798.Number of Employees Who Met the Target/README.md index 5892600bcb5e5..0fd1bd67adaeb 100644 --- a/solution/2700-2799/2798.Number of Employees Who Met the Target/README.md +++ b/solution/2700-2799/2798.Number of Employees Who Met the Target/README.md @@ -133,6 +133,22 @@ function numberOfEmployeesWhoMetTarget( } ``` +### **Rust** + +```rust +impl Solution { + pub fn number_of_employees_who_met_target(hours: Vec, target: i32) -> i32 { + let mut ans = 0; + for &v in hours.iter() { + if v >= target { + ans += 1; + } + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2798.Number of Employees Who Met the Target/README_EN.md b/solution/2700-2799/2798.Number of Employees Who Met the Target/README_EN.md index cb03a728fe859..d68eb0dd28a3e 100644 --- a/solution/2700-2799/2798.Number of Employees Who Met the Target/README_EN.md +++ b/solution/2700-2799/2798.Number of Employees Who Met the Target/README_EN.md @@ -117,6 +117,22 @@ function numberOfEmployeesWhoMetTarget( } ``` +### **Rust** + +```rust +impl Solution { + pub fn number_of_employees_who_met_target(hours: Vec, target: i32) -> i32 { + let mut ans = 0; + for &v in hours.iter() { + if v >= target { + ans += 1; + } + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2798.Number of Employees Who Met the Target/Solution.rs b/solution/2700-2799/2798.Number of Employees Who Met the Target/Solution.rs new file mode 100644 index 0000000000000..e80d100d2aa80 --- /dev/null +++ b/solution/2700-2799/2798.Number of Employees Who Met the Target/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn number_of_employees_who_met_target(hours: Vec, target: i32) -> i32 { + let mut ans = 0; + for &v in hours.iter() { + if v >= target { + ans += 1; + } + } + ans + } +} 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 a030535d4a37d..d494fb05744cf 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 @@ -299,6 +299,58 @@ function countCompleteSubarrays(nums: number[]): number { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn count_complete_subarrays(nums: Vec) -> i32 { + let mut set: HashSet<&i32> = nums.iter().collect(); + let n = nums.len(); + let m = set.len(); + let mut ans = 0; + for i in 0..n { + set.clear(); + for j in i..n { + set.insert(&nums[j]); + if set.len() == m { + ans += n - j; + break; + } + } + } + ans as i32 + } +} +``` + +```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 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]); + } + i += 1; + } + } + ans as i32 + } +} +``` + ### **...** ``` 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 42223345a032e..4092b51326657 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 @@ -271,6 +271,58 @@ function countCompleteSubarrays(nums: number[]): number { } ``` +### **Rust** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn count_complete_subarrays(nums: Vec) -> i32 { + let mut set: HashSet<&i32> = nums.iter().collect(); + let n = nums.len(); + let m = set.len(); + let mut ans = 0; + for i in 0..n { + set.clear(); + for j in i..n { + set.insert(&nums[j]); + if set.len() == m { + ans += n - j; + break; + } + } + } + ans as i32 + } +} +``` + +```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 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]); + } + i += 1; + } + } + ans as i32 + } +} +``` + ### **...** ``` 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 new file mode 100644 index 0000000000000..f10d7ee65ab8f --- /dev/null +++ b/solution/2700-2799/2799.Count Complete Subarrays in an Array/Solution.rs @@ -0,0 +1,24 @@ +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 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]); + } + i += 1; + } + } + ans as i32 + } +} diff --git a/solution/2800-2899/2800.Shortest String That Contains Three Strings/README.md b/solution/2800-2899/2800.Shortest String That Contains Three Strings/README.md index d050d6747a640..dcd27e55e5576 100644 --- a/solution/2800-2899/2800.Shortest String That Contains Three Strings/README.md +++ b/solution/2800-2899/2800.Shortest String That Contains Three Strings/README.md @@ -196,6 +196,93 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function minimumString(a: string, b: string, c: string): string { + const f = (s: string, t: string): string => { + if (s.includes(t)) { + return s; + } + if (t.includes(s)) { + return t; + } + const m = s.length; + const n = t.length; + for (let i = Math.min(m, n); i > 0; --i) { + if (s.slice(-i) === t.slice(0, i)) { + return s + t.slice(i); + } + } + return s + t; + }; + const s: string[] = [a, b, c]; + const perm: number[][] = [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ]; + let ans = ''; + for (const [i, j, k] of perm) { + const t = f(f(s[i], s[j]), s[k]); + if ( + ans === '' || + t.length < ans.length || + (t.length === ans.length && t < ans) + ) { + ans = t; + } + } + return ans; +} +``` + +### **Rust** + +```rust +impl Solution { + fn f(s1: String, s2: String) -> String { + if s1.contains(&s2) { + return s1; + } + if s2.contains(&s1) { + return s2; + } + for i in 0..s1.len() { + let s = &s1[i..]; + if s2.starts_with(s) { + let n = s.len(); + return s1 + &s2[n..]; + } + } + s1 + s2.as_str() + } + + pub fn minimum_string(a: String, b: String, c: String) -> String { + let s = [&a, &b, &c]; + let perm = [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ]; + let mut ans = String::new(); + for [i, j, k] in perm.iter() { + let r = Self::f(Self::f(s[*i].clone(), s[*j].clone()), s[*k].clone()); + if ans == "" || r.len() < ans.len() || (r.len() == ans.len() && r < ans) { + ans = r; + } + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2800.Shortest String That Contains Three Strings/README_EN.md b/solution/2800-2899/2800.Shortest String That Contains Three Strings/README_EN.md index d0ec56ea9348f..7278df54f9cda 100644 --- a/solution/2800-2899/2800.Shortest String That Contains Three Strings/README_EN.md +++ b/solution/2800-2899/2800.Shortest String That Contains Three Strings/README_EN.md @@ -182,6 +182,93 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function minimumString(a: string, b: string, c: string): string { + const f = (s: string, t: string): string => { + if (s.includes(t)) { + return s; + } + if (t.includes(s)) { + return t; + } + const m = s.length; + const n = t.length; + for (let i = Math.min(m, n); i > 0; --i) { + if (s.slice(-i) === t.slice(0, i)) { + return s + t.slice(i); + } + } + return s + t; + }; + const s: string[] = [a, b, c]; + const perm: number[][] = [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ]; + let ans = ''; + for (const [i, j, k] of perm) { + const t = f(f(s[i], s[j]), s[k]); + if ( + ans === '' || + t.length < ans.length || + (t.length === ans.length && t < ans) + ) { + ans = t; + } + } + return ans; +} +``` + +### **Rust** + +```rust +impl Solution { + fn f(s1: String, s2: String) -> String { + if s1.contains(&s2) { + return s1; + } + if s2.contains(&s1) { + return s2; + } + for i in 0..s1.len() { + let s = &s1[i..]; + if s2.starts_with(s) { + let n = s.len(); + return s1 + &s2[n..]; + } + } + s1 + s2.as_str() + } + + pub fn minimum_string(a: String, b: String, c: String) -> String { + let s = [&a, &b, &c]; + let perm = [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ]; + let mut ans = String::new(); + for [i, j, k] in perm.iter() { + let r = Self::f(Self::f(s[*i].clone(), s[*j].clone()), s[*k].clone()); + if ans == "" || r.len() < ans.len() || (r.len() == ans.len() && r < ans) { + ans = r; + } + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2800-2899/2800.Shortest String That Contains Three Strings/Solution.rs b/solution/2800-2899/2800.Shortest String That Contains Three Strings/Solution.rs new file mode 100644 index 0000000000000..1201e5f2c162a --- /dev/null +++ b/solution/2800-2899/2800.Shortest String That Contains Three Strings/Solution.rs @@ -0,0 +1,38 @@ +impl Solution { + fn f(s1: String, s2: String) -> String { + if s1.contains(&s2) { + return s1; + } + if s2.contains(&s1) { + return s2; + } + for i in 0..s1.len() { + let s = &s1[i..]; + if s2.starts_with(s) { + let n = s.len(); + return s1 + &s2[n..]; + } + } + s1 + s2.as_str() + } + + pub fn minimum_string(a: String, b: String, c: String) -> String { + let s = [&a, &b, &c]; + let perm = [ + [0, 1, 2], + [0, 2, 1], + [1, 0, 2], + [1, 2, 0], + [2, 0, 1], + [2, 1, 0], + ]; + let mut ans = String::new(); + for [i, j, k] in perm.iter() { + let r = Self::f(Self::f(s[*i].clone(), s[*j].clone()), s[*k].clone()); + if ans == "" || r.len() < ans.len() || (r.len() == ans.len() && r < ans) { + ans = r; + } + } + ans + } +}