1276. Number of Burgers with No Waste of Ingredients

本文介绍了一种解决汉堡配料问题的算法,通过分析不同汉堡所需的番茄片和奶酪片数量,找出能恰好用完所有配料的汉堡组合。该算法适用于Jumbo Burger(需4片番茄和1片奶酪)和Small Burger(需2片番茄和1片奶酪),并通过实例展示了其应用过程。
  1. Number of Burgers with No Waste of Ingredients
    Medium

61

77

Add to List

Share
Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:

Jumbo Burger: 4 tomato slices and 1 cheese slice.
Small Burger: 2 Tomato slices and 1 cheese slice.
Return [total_jumbo, total_small] so that the number of remaining tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return [].

Example 1:

Input: tomatoSlices = 16, cheeseSlices = 7
Output: [1,6]
Explantion: To make one jumbo burger and 6 small burgers we need 41 + 26 = 16 tomato and 1 + 6 = 7 cheese. There will be no remaining ingredients.
Example 2:

Input: tomatoSlices = 17, cheeseSlices = 4
Output: []
Explantion: There will be no way to use all ingredients to make small and jumbo burgers.
Example 3:

Input: tomatoSlices = 4, cheeseSlices = 17
Output: []
Explantion: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
Example 4:

Input: tomatoSlices = 0, cheeseSlices = 0
Output: [0,0]
Example 5:

Input: tomatoSlices = 2, cheeseSlices = 1
Output: [0,1]

Constraints:

0 <= tomatoSlices <= 10^7
0 <= cheeseSlices <= 10^7

鸡兔同笼问题
Jumbo Burger: 4 tomato slices and 1 cheese slice.
Small Burger: 2 Tomato slices and 1 cheese slice.

当tomatoSlices为奇数,或者tomatoSlices大于4cheeseSlices,或者tomatoSlices小于2cheeseSlices时,无解;

否则,令small burger=cheeseSlices,即所有的cheese都用来做small burger,这时会消耗tomatoSlices=2* cheeseSlices,剩余tomatoSlices-=2* cheese(2* small也一样)。

对于剩余的tomatoSlices,只能全部用来做 jumbo burger。所以对于每2片剩余的 tomatoSlices,都另外需要small burger的2片tomatoSlices 和 1片 cheeseSlices原料来制作jumbo burger。

class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        //Jumbo:4t,1c   Small:2t,1c
        vector<int>res;//res[0]表示Jumbo,res[1]表示small;
        if(tomatoSlices%2!=0||tomatoSlices>4*cheeseSlices||tomatoSlices<2*cheeseSlices) return res;
        
        int small=cheeseSlices,jumbo=0;
        tomatoSlices-=2*small;//多的部分只能给Jumbo
        if(tomatoSlices!=0) small-=tomatoSlices/2,jumbo+=tomatoSlices/2;
        res.push_back(jumbo); res.push_back(small);
        return res;
    }
};
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值