class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
res = []
for s in spells:
cnt = 0
for p in potions:
if s * p >= success:
cnt += 1
res.append(cnt)
return resWhere and are the sizes of the arrays and respectively.
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
potions.sort()
res = []
for s in spells:
l, r = 0, len(potions) - 1
idx = len(potions)
while l <= r:
m = (l + r) // 2
if s * potions[m] >= success:
r = m - 1
idx = m
else:
l = m + 1
res.append(len(potions) - idx)
return resWhere and are the sizes of the arrays and respectively.
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
n, m = len(spells), len(potions)
S = spells[:]
count = defaultdict(int)
spells.sort()
potions.sort()
j = m - 1
for i in range(n):
while j >= 0 and spells[i] * potions[j] >= success:
j -= 1
count[spells[i]] = m - j - 1
res = [0] * n
for i in range(n):
res[i] = count[S[i]]
return resWhere and are the sizes of the arrays and respectively.
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
n, m = len(spells), len(potions)
sIdx = list(range(n))
sIdx.sort(key=lambda x: spells[x])
potions.sort()
j = m - 1
res = [0] * n
for i in range(n):
while j >= 0 and spells[sIdx[i]] * potions[j] >= success:
j -= 1
res[sIdx[i]] = m - j - 1
return resWhere and are the sizes of the arrays and respectively.