You are given two positive integer arrays spells
and potions
, of length n
and m
respectively, where spells[i]
represents the strength of the i<sup>th</sup>
spell and potions[j]
represents the strength of the j<sup>th</sup>
potion.
You are also given an integer success
. A spell and potion pair is considered successful if the product of their strengths is at least success
.
Return an integer array pairs
of length n
where pairs[i]
is the number of potions that will form a successful pair with the i<sup>th</sup>
spell.
For example:
spells = [5, 1, 3], potions = [1, 2, 3, 4, 5], success = 7
The output should be [4, 0, 3]
because:
5 * [1, 2, 3, 4, 5] = [5, **10**, **15**, **20**, **25**]
. 4 pairs are successful.1 * [1, 2, 3, 4, 5] = [1, 2, 3, 4, 5]
. 0 pairs are successful.3 * [1, 2, 3, 4, 5] = [3, 6, **9**, **12**, **15**]
. 3 pairs are successful.As another example:
spells = [3, 1, 2], potions = [8, 5, 8], success = 16
The output should be [2, 0, 2]
because:
3 * [8, 5, 8] = [**24**, 15, **24**]
. 2 pairs are successful.1 * [8, 5, 8] = [8, 5, 8]
. 0 pairs are successful.2 * [8, 5, 8] = [**16**, 10, **16**]
. 2 pairs are successful.Can you implement an algorithm to solve this?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every single possible combination of spells and potions. For each spell, we'll go through every potion to see if they work together to meet a certain requirement. We simply count the successful pairings that meet this requirement.
Here's how the algorithm would work step-by-step:
def successful_pairs_brute_force(spells, potions, success):
number_of_spells = len(spells)
number_of_potions = len(potions)
successful_pairs = []
for i in range(number_of_spells):
spell = spells[i]
pairs_for_current_spell = 0
#Iterate through each potion for the current spell
for j in range(number_of_potions):
potion = potions[j]
#Checking if spell and potion are a successful pair
if (spell * potion) >= success:
pairs_for_current_spell += 1
successful_pairs.append(pairs_for_current_spell)
return successful_pairs
To efficiently count successful pairs, we'll use a combination of sorting and clever searching. Sorting allows us to quickly find potions that, when paired with a given spell, meet the success threshold without checking every single potion.
Here's how the algorithm would work step-by-step:
def successfulPairs(spells, potions, success):
number_of_potions = len(potions)
successful_pairs_count = 0
potions.sort()
for current_spell in spells:
# Determine minimum potion strength needed for success.
minimum_potion_strength = (success + current_spell - 1) // current_spell
# Binary search to find the first suitable potion.
left_index = 0
right_index = number_of_potions
while left_index < right_index:
middle_index = left_index + (right_index - left_index) // 2
if potions[middle_index] < minimum_potion_strength:
left_index = middle_index + 1
else:
right_index = middle_index
# Count successful pairs based on the search result.
successful_pairs_count += number_of_potions - left_index
return successful_pairs_count
Case | How to Handle |
---|---|
Empty spells or potions array | Return an empty list of successful pairs, since no pairs can be formed. |
spells or potions array containing negative numbers | Handle negative numbers correctly during multiplication and comparison with the success value. |
success value is zero | Consider the case where multiplying any spell and potion results in success. |
success value is very large (close to maximum integer value) | Be wary of potential integer overflow during multiplication; use long type to avoid it. |
spells or potions array containing very large numbers (close to maximum integer value) | Use long to avoid integer overflow when calculating the product of spell and potion values. |
All pairs successful (product of all spells and potions >= success) | Ensure the algorithm correctly identifies and counts all successful pairs. |
No pairs successful (product of all spells and potions < success) | Algorithm correctly returns 0 for that element of spells. |
spells and/or potions arrays are very large, impacting performance. | Sorting the potions array and using binary search provides efficient counting of successful pairs. |