diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md index 88e65ee3b9473..5dd30b7801c23 100644 --- a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md @@ -170,6 +170,29 @@ function successfulPairs(spells: number[], potions: number[], success: number): } ``` +#### Rust + +```rust +impl Solution { + pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { + potions.sort(); + let m = potions.len(); + + spells.into_iter().map(|v| { + let i = potions.binary_search_by(|&p| { + let prod = (p as i64) * (v as i64); + if prod >= success { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Less + } + }).unwrap_or_else(|x| x); + (m - i) as i32 + }).collect() + } +} +``` + diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md index 463d652b4ed1e..9ff195d0061ff 100644 --- a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md @@ -170,6 +170,29 @@ function successfulPairs(spells: number[], potions: number[], success: number): } ``` +#### Rust + +```rust +impl Solution { + pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { + potions.sort(); + let m = potions.len(); + + spells.into_iter().map(|v| { + let i = potions.binary_search_by(|&p| { + let prod = (p as i64) * (v as i64); + if prod >= success { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Less + } + }).unwrap_or_else(|x| x); + (m - i) as i32 + }).collect() + } +} +``` + diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.rs b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.rs new file mode 100644 index 0000000000000..138b9f3096dff --- /dev/null +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { + potions.sort(); + let m = potions.len(); + + spells + .into_iter() + .map(|v| { + let i = potions + .binary_search_by(|&p| { + let prod = (p as i64) * (v as i64); + if prod >= success { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Less + } + }) + .unwrap_or_else(|x| x); + (m - i) as i32 + }) + .collect() + } +}