給出一個數列spells和一個數列potions,用spells里面每個數依次乘以potions里面每個數,輸出和spells等長的數列,每個值等于用spells里面每個數乘以potions每個數得到的length(potions)個數里面大于等于給定值success的數量
先對potions按從小到大排序,然后對于spells里面每個數,二分第一個符合大于等于success的數的位置
注意邊界值
1 #2300
2 #Runtime: 1082 ms (Beats 100%)
3 #Memory: 37.5 MB (Beats 38.71%)
4
5 class Solution(object):
6 def successfulPairs(self, spells, potions, success):
7 """
8 :type spells: List[int]
9 :type potions: List[int]
10 :type success: int
11 :rtype: List[int]
12 """
13 potions.sort()
14 ans = []
15 for sp in spells:
16 ans.append(len(potions) - bisect_left(potions, ceil(1.0 * success / sp)))
17 return ans