LCP 18. 早餐组合
小扣在秋日市集选择了一家早餐摊位,一维整型数组 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
原站题解
cpp 解法, 执行用时: 292 ms, 内存消耗: 143 MB, 提交时间: 2023-09-27 14:46:07
class Solution { public: int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) { const int mod = 1e9 + 7; int 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 >= 0 && staple[i] + drinks[j] > x) j--; if (j == -1) break; ans += j + 1; ans %= mod; } return ans; } };
php 解法, 执行用时: 624 ms, 内存消耗: 34.2 MB, 提交时间: 2023-09-27 14:45:36
class Solution { /** * @param Integer[] $staple * @param Integer[] $drinks * @param Integer $x * @return Integer */ function breakfastNumber($staple, $drinks, $x) { sort($drinks); sort($staple); $cnt = 0; $m = count($staple); $n = count($drinks); $i= 0 ; $j = $n-1; while($i<$m && $j >= 0){ if( $staple[$i]+$drinks[$j] <= $x) { $cnt = ($cnt + $j + 1) % 1000000007; //+$j 代表当前元素前$j个元素都可以+1(自己) $i++; }else{ $j--; } } return $cnt%1000000007; } }
java 解法, 执行用时: 77 ms, 内存消耗: 61.2 MB, 提交时间: 2023-09-27 14:44:47
class Solution { // 双指针 public int breakfastNumber(int[] staple, int[] drinks, int x) { Arrays.sort(staple); Arrays.sort(drinks); int cnt=0; int m=staple.length,n=drinks.length; int i=0,j=n-1; while(i<m&&j>=0){ if(staple[i]+drinks[j]<=x){ cnt=(cnt+j+1)%1000000007; i++; }else{ j--; } } return cnt%1000000007; } // 二分 public int breakfastNumber2(int[] staple, int[] drinks, int x) { Arrays.sort(staple); Arrays.sort(drinks); int cnt=0; int m=staple.length,n=drinks.length; for(int i=0;i<m;i++){ int temp=x-staple[i]; int idx=binarySearch(drinks,temp); if(idx==0){ break; } cnt=(cnt+idx)%1000000007; } return cnt%1000000007; } public int binarySearch(int[] nums,int target){ int i=0,j=nums.length; while(i<j){ int mid=i+(j-i)/2; if(nums[mid]>target){ j=mid; }else{ i=mid+1; } } return i; } }
python3 解法, 执行用时: 256 ms, 内存消耗: 31.3 MB, 提交时间: 2023-09-27 14:44:00
class Solution: def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int: ans = 0 arr = [0 for i in range(x+1)] for sta in staple: if sta < x: arr[sta] += 1 for i in range(2, x): arr[i] += arr[i-1] for drink in drinks: lt = x - drink if lt <= 0: continue ans += arr[lt] return ans % (10 ** 9 + 7)
golang 解法, 执行用时: 584 ms, 内存消耗: 9.9 MB, 提交时间: 2021-05-18 15:13:36
func breakfastNumber(staple []int, drinks []int, x int) int { ans := 0 sort.Ints(staple) sort.Ints(drinks) left, right := 0, len(drinks)-1 for left < len(staple) && right >= 0 { if staple[left] + drinks[right] <= x { ans += right + 1 left++ } else { right-- } } return ans % (1e9+7) }