From 5c32355f84ff604d17d2f36491708ec2d9a1a3ab Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 28 Apr 2025 06:22:27 +0800 Subject: [PATCH] feat: add rust solution to lc prolem: No.2300 No.2300.Successful Pairs of Spells and Potions --- .../README.md | 23 +++++++++++++++++++ .../README_EN.md | 23 +++++++++++++++++++ .../Solution.rs | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.rs 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() + } +}