LeetCode 2300. 咒语和药水的成功对数

原题链接:2300. 咒语和药水的成功对数

二分查找

C++代码

class Solution {
public:
    vector successfulPairs(vector& spells, vector& potions, long long success) {
        vector successfulPairs;
        sort(potions.begin(),potions.end());
        int size = potions.size();
        for(int spell:spells){
            double temp = success*1.0/spell;
            long long x;
            if(temp == (long long)temp){
                x = (long long)temp;
            }else{
                x = (long long)temp+1;
            }
            int pos = lower_bound(potions.begin(),potions.end(),x)-potions.begin();
            successfulPairs.push_back(size-pos);
        }
        return successfulPairs;
    }
};

你可能感兴趣的:(LeetCode,算法,leetcode,职场和发展)