(简单)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
解题
方法一
- 将staple和drinks分别按从小到大排序
- i指向staple的头,j指向drinks的尾
- 循环:当staple[i] + drinks[j] < x 时,说明有j + 1个是可以的,此时将i += 1,并将 ans = ans + j + 1;当staple[i] + drinks[j] > x 时,说明大了,此时将j -= 1
class Solution:
def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int:
staple.sort()
drinks.sort()
count, i , j = 0, 0, len(drinks) - 1
while i < len(staple) and j >= 0:
if staple[i] + drinks[j] <= x:
count = count + j + 1
i = i + 1
else:
j = j - 1
return count % (10 ** 9 + 7)
方法二
- 构造num数组,num[i]表示主食中价格小于等于i的个数
- 遍历drinks,如果当前饮料的价格 < x ,则num[x - 当前饮料的价格]表示当前饮料可以和主食的组合数,否则当前饮料的价格已经超过了上限
class Solution:
def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int:
count = 0
num = [0 for i in range(x+1)]
for i in staple:
if i <= x:
num[i] = num[i] + 1
for i in range(1,x+1):
num[i] = num[i-1] + num[i]
for i in drinks:
if i < x:
count = count + num[x - i]
return count % (10 ** 9 + 7)
方法三
- 先进行排序,方便二分查找
- 第一个二分查找获取买主食花费的最大金额,因为买食物后剩余的钱必须足够买饮料
- 第二个二分查找获取买饮料花费的最大金额
class Solution:
def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int:
count = 0
staple.sort()
drinks.sort()
l = self.BinarySearch(staple,x)
for i in staple[:l+1]:
if self.BinarySearch(drinks,x - i) != -1:
count += self.BinarySearch(drinks,x - i) + 1
return count % (10 ** 9 + 7)
def BinarySearch(self, lists: List[int], x:int) -> int:
begin, end= 0, len(lists) - 1
if lists[begin] > x :
return -1
if lists[end] <= x:
return len(lists) - 1
mid = (begin + end) // 2
while begin <= end:
if lists[mid] == x:
for i in range(mid+1,len(lists)):
if lists[i] == x:
mid = i
else:
return mid
elif lists[mid] > x:
end = mid - 1
elif lists[mid] < x:
begin = mid + 1
mid = (begin + end) // 2
return mid