题目描述
小扣在秋日市集选择了一家早餐摊位,一维整型数组 staple 中记录了每种主食的价格,
一维整型数组 drinks 中记录了每种饮料的价格。
小扣的计划选择一份主食和一款饮料,且花费不超过 x 元。
请返回小扣共有多少种购买方案。
注意:答案需要以 1e9 + 7 (1000000007) 为底取模,
如:计算初始结果为:1000000008,请返回 1
示例 1:
输入:staple = [10,20,5], drinks = [5,5,2], x = 15
输出:6
解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[0] = 10 + 5 = 15;
第 2 种方案:staple[0] + drinks[1] = 10 + 5 = 15;
第 3 种方案:staple[0] + drinks[2] = 10 + 2 = 12;
第 4 种方案:staple[2] + drinks[0] = 5 + 5 = 10;
第 5 种方案:staple[2] + drinks[1] = 5 + 5 = 10;
第 6 种方案:staple[2] + drinks[2] = 5 + 2 = 7。
示例 2:
输入:staple = [2,1,1], drinks = [8,9,5,1], x = 9
输出:8
解释:小扣有 8 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
第 1 种方案:staple[0] + drinks[2] = 2 + 5 = 7;
第 2 种方案:staple[0] + drinks[3] = 2 + 1 = 3;
第 3 种方案:staple[1] + drinks[0] = 1 + 8 = 9;
第 4 种方案:staple[1] + drinks[2] = 1 + 5 = 6;
第 5 种方案:staple[1] + drinks[3] = 1 + 1 = 2;
第 6 种方案:staple[2] + drinks[0] = 1 + 8 = 9;
第 7 种方案:staple[2] + drinks[2] = 1 + 5 = 6;
第 8 种方案:staple[2] + drinks[3] = 1 + 1 = 2;
提示:
1 <= staple.length <= 10^5
1 <= drinks.length <= 10^5
1 <= staple[i],drinks[i] <= 10^5
1 <= x <= 2*10^5
算法1
(暴力枚举) $O(n^2)$
暴力 第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;
}
};
算法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;
}
};
使用 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;
}
};
算法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;
}
};
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;
}
};