NeetCode #274LC-2300MediumBinary Search
← Back to All Problems#274 · #2300 · Successful Pairs of Spells and Potions(咒语和药水的成功对数)
📌 Problem Statement & Constraints
You are given two positive integer arrays
spells and potions, and an integer success. A spell and a potion form a successful pair if their product is at least success. Return an array where pairs[i] is the number of potions that form a successful pair with spells[i]. Constraints: 1 <= n, m <= 10^5, 1 <= spells[i], potions[i] <= 10^5, 1 <= success <= 10^10.💡 Core Algorithmic Approaches
- Sort
potions. For each spell, the required minimum potion strength isceil(success / spell). - Then a binary search (
bisect_left) finds the first potion at least that strong, and the count ism - index. - This is O((n + m) log m), far better than the O(n * m) brute force.
- Computing the required strength with integer arithmetic avoids floating-point issues.
💻 Benchmark Python3 Implementation
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
from bisect import bisect_left
potions.sort()
m = len(potions)
res = []
for s in spells:
need = (success + s - 1) // s # ceiling of success / s
idx = bisect_left(potions, need) # first potion strong enough
res.append(m - idx)
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O(m log m + n log m): sorting the potions plus a binary search per spell.
💾 Space Complexity
O(1) beyond the output (the sort is in place).
⚠️ Interview Pitfalls & Follow-ups
- Using
success / sandmath.ceil: floating-point precision breaks for values near 10^10; the integer ceiling(success + s - 1) // sis exact. - Forgetting to sort the potions: the binary search requires sorted input.
- Counting the potions below the threshold instead of above: the answer is
m - idx. - Nested loops: O(n * m) = 10^10 operations, which times out.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
咒语和药水的成功对数
给你两个正整数数组 spells 和 potions,长度分别为 n 和 m,其中 spells[i] 表示第 i 个咒语的能量强度, potions[j] 表示第 j 瓶药水的能量强度。同时给你一个整数 success。一个咒语和药水的能量强度相乘如果 大于等于 success,那么它们视为一对成功的组合。请你返回一个长度为 n 的整数数组 pairs,其中 pairs[i] 是能跟第 i 个咒语成功组合的药水数目。约束:1 ≤ n, m ≤ 10^5;1 ≤ spells[i], potions[j] ≤ 10^5;1 ≤ success ≤ 10^10。
核心要点
- 排序 + 二分(标准解) :把 potions 升序排序,对每个咒语二分找到「第一个满足 spell × potion >= success 的位置」,答案为 m − 该位置 。
- 避免浮点与溢出 :不要计算 spell × potion (在 C++/Java 中可达 10^10 会溢出 int),而是先算出所需的最小药水值 need = ceil(success / spell) = (success + spell − 1) // spell 。
- 二分查找 need 的插入位置 :用 bisect_left(potions, need) ,它返回第一个 ≥ need 的位置。
- 复杂度 :排序 O(m log m),每个咒语 O(log m),总计 O((n + m) log m)。
常见陷阱
- 用 success / s 浮点除法后 ceil :会引入浮点误差(success 可达 10^10)。必须用整数向上取整 (success + s − 1) // s 。
- 直接比较 spell * potion >= success :在 C++/Java 中乘积可达 10^10,超出 int 范围会溢出。Python 无此问题,但用 need 的写法更通用。
- 用 bisect_right 而非 bisect_left :需要「第一个 ≥ need 的位置」, bisect_left 正确。
- 答案写成 idx :满足条件的药水是 potions[idx..m−1] ,个数为 m − idx 。
参考实现
from bisect import bisect_left
class Solution:
def successfulPairs(self, spells: list[int], potions: list[int], success: int) -> list[int]:
potions.sort()
m = len(potions)
res = []
for s in spells:
# 所需的最小药水强度(向上取整,避免浮点与溢出)
need = (success + s - 1) // s
idx = bisect_left(potions, need)
res.append(m - idx) # 从 idx 到末尾都满足
return res