# 1276. Number of Burgers with No Waste of Ingredients

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 4*1 + 2*6 = 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.
``````

Constraints:

• `0 <= tomatoSlices, cheeseSlices <= 107`

``````class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
for (int i = 0; i <= cheeseSlices; ++i) {
if (i * 4 + (cheeseSlices - i) * 2 == tomatoSlices) {
return {i, cheeseSlices - i};
}
}
return {};
}
};
``````

`4x + 2y = t`
`x + y = c`

`x = t / 2 - c`
`y = 2c - t / 2`

`t / 2 - c >= 0`
`2c - t / 2 >= 0`

``````class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
int t = tomatoSlices, c = cheeseSlices;
return (t % 2 == 0 && t >= 2 * c && t <= 4 * c) ? vector<int>{t / 2 - c, 2 * c - t / 2} : vector<int>();
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1276

https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/

https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/441475/The-only-realistic-solution

https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/441321/JavaC%2B%2BPython-Chickens-and-Rabbits

LeetCode All in One 题目讲解汇总(持续更新中…)

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation