SDE & MLE Prep

2300. Successful Pairs of Spells and Potions

Problem

A spell and a potion form a successful pair when the product of their strengths is at least success. For each spell, return how many potions it pairs successfully with.

Example: spells = [5,1,3], potions = [1,2,3,4,5], success = 7 → [4,0,3]

Constraints: up to 10⁵ spells and potions.

Sort the potions once. A pair succeeds when potion ≥ ⌈success / spell⌉, so the count for each spell is m − lower_bound(⌈success / spell⌉). Integer ceiling avoids floating-point trouble.

Complexity: O((n + m) log m).

class Solution:
    def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
        potions = sorted(potions)
        m = len(potions)
        return [m - bisect.bisect_left(potions, (success + s - 1) // s) for s in spells]