LeetCode Entry
2300. Successful Pairs of Spells and Potions
Number of potions spells[i] bigger than success
2300. Successful Pairs of Spells and Potions medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1136
Problem TLDR
Number of potions * spells[i] bigger than success #midium #bs #sort
Intuition
Sort potions. Binary search value spells[i] * potions[j] < success.
Approach
- or search for
success + spells - 1 / spellswithoud converting to double
Complexity
-
Time complexity: \(O(nlog(n))\)
-
Space complexity: \(O(n)\)
Code
// 105ms
fun successfulPairs(sp: IntArray, p: IntArray, s: Long) = run { p.sort()
sp.map { v -> 1 + p.size + p.asList().binarySearch{ if (1L*it*v < s) -1 else 1 }}}
// 17ms
pub fn successful_pairs(sp: Vec<i32>, mut p: Vec<i32>, s: i64) -> Vec<i32> {
p.sort_unstable(); let l = p.len() as i32;
sp.iter().map(|&v| l - p.partition_point(|&p| v as i64 * (p as i64) < s) as i32).collect()
}