1276. Number of Burgers with No Waste of Ingredients
Description
Given two integers tomatoSlices
and cheeseSlices
. The ingredients of different burgers are as follows:
- Jumbo Burger:
4
tomato slices and1
cheese slice. - Small Burger:
2
Tomato slices and1
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
Solutions
Solution 1: Mathematics
We set the number of Jumbo Burgers as $x$ and the number of Small Burgers as $y$, then we have:
$$ \begin{aligned} 4x + 2y &= tomatoSlices \ x + y &= cheeseSlices \end{aligned} $$
Transforming the above two equations, we can get:
$$ \begin{aligned} y = (4 \times cheeseSlices - tomatoSlices) / 2 \ x = cheeseSlices - y \end{aligned} $$
Where $x$ and $y$ must be non-negative integers.
The time complexity is $O(1)$, and the space complexity is $O(1)$.
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|