6096 20220611夜周赛第二题

https://leetcode.cn/problems/successful-pairs-of-spells-and-potions/

  1. class Solution:
  2. def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
  3. # potions.sort()
  4. # return [len(potions) - bisect_left(potions, (success + x - 1) // x) for x in spells]
  5. potions.sort()
  6. array = []
  7. def binarySearch(array: List[int], k: int) -> int:
  8. left = 0
  9. right = len(array) - 1
  10. while left <= right:
  11. mid = (left + right) // 2
  12. if array[mid] == k:
  13. return mid
  14. elif array[mid] > k:
  15. left = mid + 1
  16. else:
  17. right = mid - 1
  18. for i in spells:
  19. array.append(len(potions) - binarySearch(potions, math.ceil(success // i)))
  20. return array

mission

  1. 学习库函数 bisect.left()的参数以及用法,将其入库
  2. https://docs.python.org/zh-cn/3.10/library/bisect.html
  3. 搞懂 灵茶山乐府版本 x - 1的原因