Counting Successful Spell-Potion Pairs Using Binary Search (100% Runtime Efficiency)
Intuition\nThe function calculates the number of successful pairs between each spell and a potion. A pair is considered successful if the product of the spell a
# Intuition\nThe function calculates the number of successful pairs between each spell and a potion. A pair is considered successful if the product of the spell and potion values is greater than or equal to a given success threshold. To efficiently find these pairs, binary search is employed after sorting the potions array.\n\n\n# Approach\n1. **Sort Potions**: Begin by sorting the `potions` array. This allows for efficient searching using binary search.\n2. **Binary Search for Each Spell**:\n    - For each spell, determine the smallest index in the `potions` array such that the product of the spell and the potion at that index meets or exceeds the success threshold.\n    - Use binary search to find this index efficiently. The idea is to minimize the number of comparisons by halving the search space in each iteration.\n    - The number of successful pairs for each spell is then the total number of potions minus the found index.\n3. **Store Results**: Append the result for each spell to the `ans` array.\n\n\n# Complexity\n- **Time complexity**: $O(nlogm)$, where n is the number of spells and m is the number of potions. Sorting the potions array takes $O(mlogm)$, and each binary search operation for a spell is $O(logm)$, leading to the overall complexity.\n\n- **Space complexity**: $O(1)$ for the binary search and $O(n)$ for storing the results in the `ans` array.\n\n# Code\n```\nfunction successfulPairs(spells: number[], potions: number[], success: number): number[] {\n    potions.sort((a, b) => a - b);\n    const m = potions.length;\n    const ans: number[] = [];\n    for (const v of spells) {\n        let left = 0;\n        let right = m;\n        while (left < right) {\n            const mid = (left + right) >> 1;\n            if (v * potions[mid] >= success) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n        ans.push(m - left);\n    }\n    return ans;\n}\n```
['TypeScript']