力扣 中等 2300.咒语和药水的成功对数
文章目录
- 题目介绍
- 解法
题目介绍
解法
class Solution {
public int[] successfulPairs(int[] spells, int[] potions, long success){
Arrays.sort(potions);
int n = spells.length, m = potions.length;
int[] pairs = new int[n];
for (int i = 0; i < n; i++) {
int left = 0, right = m - 1;
long factor = spells[i];
while (left <= right) {
int mid = left + (right - left) / 2;
if ((long) potions[mid] * factor < success) {
left = mid + 1;
} else {
right = mid - 1;
}
}
pairs[i] = m - left;
}
return pairs;
}
}