Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1276.html
1276. Number of Burgers with No Waste of Ingredients
Level
Medium
Description
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
Solution
Since one jumbo burger uses 4 tomato slices and one small burger uses 2 tomato slices, the number of tomato slices used in total must be even. Therefore, first check whether tomatoSlices
is even. If tomatoSlices
is odd, then it is not possible to use all the gradients, so return []
.
Then use the idea of linear equations to figure out the number of jumbo burgers and small burgers. Return the result if the numbers of both burgers are nonnegative integers.
-
class Solution { public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) { List<Integer> numbers = new ArrayList<Integer>(); if (tomatoSlices % 2 != 0) return numbers; int halfTomatoSlices = tomatoSlices / 2; int totalJumbo = halfTomatoSlices - cheeseSlices; int totalSmall = cheeseSlices * 2 - halfTomatoSlices; if (totalJumbo >= 0 && totalSmall >= 0) { numbers.add(totalJumbo); numbers.add(totalSmall); } return numbers; } }
-
// OJ: https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/ // Time: O(1) // Space: O(1) class Solution { public: vector<int> numOfBurgers(int T, int C) { int x = T - 2 * C; if (x % 2 || x < 0) return {}; x /= 2; if (C - x < 0) return {}; return { x, C - x }; } };
-
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: k = 4 * cheeseSlices - tomatoSlices y = k // 2 x = cheeseSlices - y return [] if k % 2 or y < 0 or x < 0 else [x, y] ############ # 1276. Number of Burgers with No Waste of Ingredients # https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/ class Solution: def numOfBurgers(self, t: int, c: int) -> List[int]: a = (t-2*c) / 2 b = c - a return [int(a),int(b)] if int(a) == a and int(b) == b and a >= 0 and b >= 0 else []
-
func numOfBurgers(tomatoSlices int, cheeseSlices int) []int { k := 4*cheeseSlices - tomatoSlices y := k / 2 x := cheeseSlices - y if k%2 != 0 || x < 0 || y < 0 { return []int{} } return []int{x, y} }