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   itdef  阅读(275)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2018-09-13 谷歌开源的一个BTREE实现 Go语言
2018-09-13 分布式协议学习笔记(二) 日志复制

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示