You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.
You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at leastsuccess.
Return an integer arraypairsof lengthnwherepairs[i]is the number of potions that will form a successful pair with theithspell.
Question reduces to finding no of potions for each spell which have product greater than success where product is (each spell * potions[i]). Now decision is whether to keep the potions array sorted or not.
The most straightforward approach is to check every possible pair of spell and potion. For each spell, we iterate through all potions and count how many pairs have a product greater than or equal to the given success threshold. This method does not require sorting and works directly with the input arrays.
To improve efficiency, we sort the potions array. For each spell, we want to count how many potions can form a successful pair (i.e., spell * potion >= success). Since the potions are sorted, we can use binary search to quickly find the first potion that meets the threshold for each spell.
import java.util.*;
classSolution {
publicint[]successfulPairs(int[] spells, int[] potions, long success) {
Arrays.sort(potions);
int m = potions.length;
int[] res =newint[spells.length];
for (int i = 0; i < spells.length; i++) {
int left = 0, right = m;
while (left < right) {
int mid = left + (right - left) / 2;
if ((long)spells[i]* potions[mid]< success) left = mid + 1;
else right = mid;
}
res[i]= m - left;
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funsuccessfulPairs(spells: IntArray, potions: IntArray, success: Long): IntArray {
potions.sort()
val m = potions.size
return spells.map { spell ->var left = 0var right = m
while (left < right) {
val mid = left + (right - left) / 2if (spell.toLong() * potions[mid] < success) left = mid + 1else right = mid
}
m - left
}.toIntArray()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from bisect import bisect_left
classSolution:
defsuccessfulPairs(self, spells: list[int], potions: list[int], success: int) -> list[int]:
potions.sort()
m = len(potions)
res = []
for spell in spells:
# Find the smallest index where spell * potion >= success left, right =0, m
while left < right:
mid = (left + right) //2if spell * potions[mid] < success:
left = mid +1else:
right = mid
res.append(m - left)
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnsuccessful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
potions.sort();
let m = potions.len();
spells.iter().map(|&spell| {
let (mut left, mut right) = (0, m);
while left < right {
let mid = left + (right - left) /2;
if (spell asi64) * (potions[mid] asi64) < success {
left = mid +1;
} else {
right = mid;
}
}
(m - left) asi32 }).collect()
}
}
⏰ Time complexity: O(n log m), where n is the number of spells and m is the number of potions. Sorting potions takes O(m log m), and for each spell, binary search takes O(log m).
🧺 Space complexity: O(n), for the result array storing the count for each spell.