LeetCode75-2300-Successful-Pairs-of-Spells-and-Potions

🧠 Summary of Thoughts

Sort + Binary Search for the first valid threshold

  1. For each spell, count how many potions satisfy: spell * potion >= success
  2. If a potion works, then all stronger potions will also work. So this actually transfer the quesiton to asking what is the index of threshold (Binary Search)
  3. potion >= ceil(success / spell)
    • ceil(a / b) = (a + b - 1) / b

✅ Code(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int[] successfulPairs(int[] spells, int[] potions, long success) {
Arrays.sort(potions);
int[] pairs = new int[spells.length];
int m = potions.length;
for (int i = 0; i < spells.length; i++) {
long need = (success + spells[i] - 1) / spells[i];
int left = 0;
int right = m - 1;
int idx = m;
while (left <= right) {
int mid = left + (right - left) / 2;
if (need <= potions[mid]) {
idx = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
pairs[i] = m - idx;
}
return pairs;
}
}

LeetCode75-2300-Successful-Pairs-of-Spells-and-Potions
https://powersixxx.github.io/leetcode-blog/2026/04/09/LeetCode75-2300-Successful-Pairs-of-Spells-and-Potions/
作者
Baowen Liu
发布于
2026年4月9日
许可协议