Skip to content

Successful Pairs of Spells and Potions

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

You have two lists of positive integers, spells and potions, and a number success. A spell and a potion form a successful pair when the product of their strengths is at least success. For each spell, count how many potions it pairs with successfully, and return the counts as a list.

spells = [5, 1, 3], potions = [1, 2, 3, 4, 5], success = 7   →  [4, 0, 3]
spells = [3, 1, 2], potions = [8, 5, 8],       success = 16  →  [2, 0, 2]

In the first example, spell 5 works with potions 2, 3, 4 and 5 (products 10 to 25), spell 1 with none, and spell 3 with potions 3, 4 and 5.

Constraints

  • 1 <= spells.length, potions.length <= 10^5
  • 1 <= spells[i], potions[i] <= 10^5
  • 1 <= success <= 10^10