Problem Solving/LeetCode
2023.04.02 Today's Challenge
fabichoi
2023. 4. 2. 23:45
https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
Successful Pairs of Spells and Potions - LeetCode
Can you solve this real interview question? Successful Pairs of Spells and Potions - 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] repre
leetcode.com
무식하게 풀면 바로 TL 나옴.
정렬도 했는 TL.
정렬 + 이분 탐색 해야 통과됨.
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
res = []
l = len(potions)
potions.sort()
max_potion = potions[l-1]
for spell in spells:
min_potion = (success + spell - 1) // spell
if min_potion > max_potion:
res.append(0)
continue
idx = bisect.bisect_left(potions, min_potion)
res.append(l - idx)
return res
반응형