LeetCode 1276: Number of Burgers with No Waste of Ingredients
Problem Description
Explanation:
To solve this problem, we can set up a system of linear equations based on the given constraints. Let x be the number of jumbo burgers and y be the number of small burgers. We can then form the following equations:
- 4x + 2y = tomatoSlices
- x + y = cheeseSlices
We can solve these equations to find the values of x and y that satisfy both constraints. If x and y are integers and non-negative, we return [x, y]; otherwise, we return an empty list. :
Solutions
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
List<Integer> result = new ArrayList<>();
if ((tomatoSlices - 2 * cheeseSlices) % 2 != 0) {
return result;
}
int x = (tomatoSlices - 2 * cheeseSlices) / 2;
int y = cheeseSlices - x;
if (x >= 0 && y >= 0 && 4 * x + 2 * y == tomatoSlices) {
result.add(x);
result.add(y);
}
return result;
}
}
Loading editor...