LeetCode lcp018. 早餐组合 二分

地址 https://leetcode-cn.com/problems/2vYnGI/

算法1
(暴力枚举) O(n2)O(n2)
暴力 第52个数据超时了

C++ 代码

class Solution {
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
        long long ans =0;
        for(int i = 0;i < staple.size();i++){
            for(int j =0;j< drinks.size();j++){
                if(staple[i] + drinks[j] <=x ){
                    ans = (ans+1)%1000000007;
                }   
            }
        }

        return ans;
    }
};

作者:itdef
链接:https://www.acwing.com/solution/content/20660/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

算法2
我们将算法1优化,数据进行排序,然后使用二分查找定位,能将
时间优化到 log级别

C++ 代码

class Solution {
public:
    bool check(vector<int>& drinks, int find,int m) {
    if (drinks[m] > find) return false;
    return true;
}

int bsearch_2(vector<int>& drinks,int find,int l, int r)
{
    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (check(drinks,find,mid)) l = mid;
        else r = mid - 1;
    }

    return l;
}

int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
    long long  count = 0;
    sort(staple.begin(), staple.end());
    sort(drinks.begin(), drinks.end());
    for (int i = 0; i < staple.size(); i++) {
        if (staple[i] > x) break;
        int findx = x - staple[i];
        int idx = bsearch_2(drinks, findx, 0, drinks.size()-1);
        if (drinks[idx] <= findx) {
            idx++;
        }
        count += idx;
        count = count%1000000007;
    }

    return count;
}
};

作者:itdef
链接:https://www.acwing.com/solution/content/20660/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

使用 stl的二分函数 代码会简洁一点

class Solution {
public:

int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
    long long  count = 0;
    sort(staple.begin(), staple.end());
    sort(drinks.begin(), drinks.end());
    for (int i = 0; i < staple.size(); i++) {
        if (staple[i] > x) break;
        int findx = x - staple[i];
        int idx = upper_bound( drinks.begin(),drinks.end(),findx ) -drinks.begin();
        count = (count+idx)%1000000007;
    }

    return count;
}
};

作者:itdef
链接:https://www.acwing.com/solution/content/20660/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

算法3
数据排序后 确认staple的数字,在drinks数组中查找时候,可以依靠数组的单调性避免一些无谓的搜索.
staple由大到小排序, drinks由小到大排序
查找完staple的元素x ,得到drinks的元素y,那么在进行x+1的元素处理时候,y的元素肯定是符合要求的,
不必重复检测

C++ 代码

class Solution {
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
        long long ans =0;
        sort(staple.begin(),staple.end(),greater<int>());
        sort(drinks.begin(),drinks.end());
        int j =0;
        for(int i = 0;i <staple.size();i++){
            while(j<drinks.size()&&  staple[i]+drinks[j] <=x ) j++;
            ans = (ans+j)%1000000007;
        }
        return ans;
    }
};

作者:itdef
链接:https://www.acwing.com/solution/content/20660/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
   int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
    long long ans = 0;
    sort(staple.begin(), staple.end());
    sort(drinks.begin(), drinks.end());
    int j = drinks.size() - 1;
    for (int i = 0; i < staple.size(); i++) {
        while (j<drinks.size() && staple[i] + drinks[j] > x) j--;
        if(j>=0)
            ans = (ans + j+1) % 1000000007;
    }
    return ans;
}
};

作者:itdef
链接:https://www.acwing.com/solution/content/20660/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted on 2020-09-13 23:29  itdef  阅读(273)  评论(0编辑  收藏  举报

导航