1276. 不浪费原料的汉堡制作方案

1276. 不浪费原料的汉堡制作方案

难度: 中等

题目大意:

给你两个整数 tomatoSlicescheeseSlices,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:

  • **巨无霸汉堡:**4 片番茄和 1 片奶酪
  • **小皇堡:**2 片番茄和 1 片奶酪

请你以 [total_jumbo, total_small]([巨无霸汉堡总数,小皇堡总数])的格式返回恰当的制作方案,使得剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量都是 0

如果无法使剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量为 0,就请返回 []

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

思路

很显然这题是数学问题,我们假设有x个巨无霸汉堡,y个小皇包,那么肯定满足
{ 4 x + y = t o m a t o S l i c e s x + y = c h e e s e S l i c e s \begin{cases} 4x + y = tomatoSlices \\ x + y = cheeseSlices \end{cases} {4x+y=tomatoSlicesx+y=cheeseSlices
很容易解得:
{ x = t o m a t o S l i c e s − 2 ∗ c h e e s e S l i c e s 2 y = c h e e s e S l i c e s − x \begin{cases} x = \frac{tomatoSlices - 2*cheeseSlices}{2} \\ y = cheeseSlices - x \end{cases} {x=2tomatoSlices2cheeseSlicesy=cheeseSlicesx
那么我们只需要判断tomatoSlices - 2*cheeseSlices是不是2的倍数,并且保证数据都要大于等于0就好了

代码实现:

class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        int b = tomatoSlices - 2 * cheeseSlices;
        int a = b >> 1;
        if (a * 2 == b && a >= 0 && cheeseSlices - a >= 0) {
            return {a, cheeseSlices - a};
        }
        return {};
    }
};

时间复杂度: O ( 1 ) O(1) O(1)

结束了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值