99网
您的当前位置:首页1276. Number of Burgers with No Waste of Ingredients

1276. Number of Burgers with No Waste of Ingredients

来源:99网

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;
    }
};

因篇幅问题不能全部显示,请点此查看更多更全内容