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 5dd30b7801c23..e2494dc7a3c6d 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 @@ -120,12 +120,14 @@ class Solution { class Solution { public: vector successfulPairs(vector& spells, vector& potions, long long success) { - sort(potions.begin(), potions.end()); + ranges::sort(potions); + const int m = potions.size(); vector ans; - int m = potions.size(); - for (int& v : spells) { - int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin(); - ans.push_back(m - i); + ans.reserve(spells.size()); + + for (int v : spells) { + auto it = ranges::lower_bound(potions, static_cast(success) / v); + ans.push_back(m - static_cast(it - potions.begin())); } return ans; } @@ -153,19 +155,13 @@ function successfulPairs(spells: number[], potions: number[], success: number): potions.sort((a, b) => a - b); const m = potions.length; const ans: number[] = []; + for (const v of spells) { - let left = 0; - let right = m; - while (left < right) { - const mid = (left + right) >> 1; - if (v * potions[mid] >= success) { - right = mid; - } else { - left = mid + 1; - } - } - ans.push(m - left); + const targetPotion = success / v; + const idx = _.sortedIndexBy(potions, targetPotion, p => p); + ans.push(m - idx); } + return ans; } ``` @@ -177,18 +173,15 @@ impl Solution { pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { potions.sort(); let m = potions.len(); + let mut ans = Vec::with_capacity(spells.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() + for &v in &spells { + let target = (success + v as i64 - 1) / v as i64; + let idx = potions.partition_point(|&p| (p as i64) < target); + ans.push((m - idx) as i32); + } + + ans } } ``` 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 9ff195d0061ff..466ecb4b6cec9 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 @@ -47,8 +47,8 @@ Thus, [4,0,3] is returned. Output: [2,0,2] Explanation: - 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful. -- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. -- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. +- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. +- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. Thus, [2,0,2] is returned. @@ -120,12 +120,14 @@ class Solution { class Solution { public: vector successfulPairs(vector& spells, vector& potions, long long success) { - sort(potions.begin(), potions.end()); + ranges::sort(potions); + const int m = potions.size(); vector ans; - int m = potions.size(); - for (int& v : spells) { - int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin(); - ans.push_back(m - i); + ans.reserve(spells.size()); + + for (int v : spells) { + auto it = ranges::lower_bound(potions, static_cast(success) / v); + ans.push_back(m - static_cast(it - potions.begin())); } return ans; } @@ -153,19 +155,13 @@ function successfulPairs(spells: number[], potions: number[], success: number): potions.sort((a, b) => a - b); const m = potions.length; const ans: number[] = []; + for (const v of spells) { - let left = 0; - let right = m; - while (left < right) { - const mid = (left + right) >> 1; - if (v * potions[mid] >= success) { - right = mid; - } else { - left = mid + 1; - } - } - ans.push(m - left); + const targetPotion = success / v; + const idx = _.sortedIndexBy(potions, targetPotion, p => p); + ans.push(m - idx); } + return ans; } ``` @@ -177,18 +173,15 @@ impl Solution { pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { potions.sort(); let m = potions.len(); + let mut ans = Vec::with_capacity(spells.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() + for &v in &spells { + let target = (success + v as i64 - 1) / v as i64; + let idx = potions.partition_point(|&p| (p as i64) < target); + ans.push((m - idx) as i32); + } + + ans } } ``` diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp index 57e225f0ebb04..289550ed9d260 100644 --- a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp @@ -1,13 +1,15 @@ class Solution { public: vector successfulPairs(vector& spells, vector& potions, long long success) { - sort(potions.begin(), potions.end()); + ranges::sort(potions); + const int m = potions.size(); vector ans; - int m = potions.size(); - for (int& v : spells) { - int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin(); - ans.push_back(m - i); + ans.reserve(spells.size()); + + for (int v : spells) { + auto it = ranges::lower_bound(potions, static_cast(success) / v); + ans.push_back(m - static_cast(it - potions.begin())); } return ans; } -}; \ No newline at end of file +}; 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 index 138b9f3096dff..04ac86dd32112 100644 --- 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 @@ -2,22 +2,14 @@ impl Solution { pub fn successful_pairs(spells: Vec, mut potions: Vec, success: i64) -> Vec { potions.sort(); let m = potions.len(); + let mut ans = Vec::with_capacity(spells.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() + for &v in &spells { + let target = (success + v as i64 - 1) / v as i64; + let idx = potions.partition_point(|&p| (p as i64) < target); + ans.push((m - idx) as i32); + } + + ans } } diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts index 28ee99ab13316..9333ce665abad 100644 --- a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts @@ -2,18 +2,12 @@ function successfulPairs(spells: number[], potions: number[], success: number): potions.sort((a, b) => a - b); const m = potions.length; const ans: number[] = []; + for (const v of spells) { - let left = 0; - let right = m; - while (left < right) { - const mid = (left + right) >> 1; - if (v * potions[mid] >= success) { - right = mid; - } else { - left = mid + 1; - } - } - ans.push(m - left); + const targetPotion = success / v; + const idx = _.sortedIndexBy(potions, targetPotion, p => p); + ans.push(m - idx); } + return ans; }