单细胞的我/ 除法 转换 想不到的。最终只需要操纵potions数组 呜呜,转换,这是数学题?
spells[i]*potions[j]>=success, 返回与第i个咒语成功组合的药水数目。一个数组。pairs. potions[j]>=succes/spells[i]向上取整,然后转换为二分查找,potions数组中大于等于这个数的下标。 先排序。对Potions数组。
我的 冗长 消耗内存 125.4mb的解答 。。。
class Solution {
public: int lower_bound(vector<int>&array,long long target){
int left=-1,right=array.size();
while(left+1<right){
int mid= left+(right-left)/2;
if(array[mid]<target){
left=mid; }
else{ right=mid; } }
return right; }
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success)
{ sort(potions.begin(),potions.end());
vector<int> res;
for(int i=0;i<spells.size();i++)
{ long long target=(success+spells[i]-1)/spells[i];
int index=potions.size()-lower_bound(potions,target);
res.push_back(index);
}
return res;
}
};q_q,原来可以使用别人的库函数吗。。
大佬的解答:
排序这一行可以简写ranges::sort(potions);
for 循环 您的写法古老了一点,
for (int &i :spells)
怎么就想到了二分呢?两个数组