題目會給我們兩個輸入陣列spells咒語、potions藥水,還有一個參數success。
當咒語和藥水相乘的值 >= success就是一個成功配對。
請問每個咒語能夠形成的成功配對數有多少?
以陣列的形式輸出返回答案。
Example 1:
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
Output: [4,0,3]
Explanation:
- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
Thus, [4,0,3] is returned.
Example 2:
Input: spells = [3,1,2], potions = [8,5,8], success = 16
Output: [2,0,2]
Explanation:
- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
Thus, [2,0,2] is returned.
Constraints:
n == spells.length
咒語陣列的長度為n。
m == potions.length
藥水陣列的長度為m。
1 <= n, m <= 10^5
輸入陣列的長度都介於1~十萬。
1 <= spells[i], potions[i] <= 10^5
輸入陣列的元素值都介於1~十萬。
1 <= success <= 10^10
參數success介於1~一百億。
除了傳統的暴力搜索配對之外,還有一個比較高效率的排序+二分搜尋演算法。
先把potion藥水陣列從小到大排序,
這是為了後續的二分搜尋鋪路,滿足已排序的性質。