class Solution:
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int):
# Let x = jumbo, y = small
# 4x + 2y = tomatoSlices
# x + y = cheeseSlices
# Solve:
# x = (tomatoSlices - 2*cheeseSlices) // 2
# y = cheeseSlices - x
x = tomatoSlices - 2 * cheeseSlices
if x < 0 or x % 2 != 0:
return []
x //= 2
y = cheeseSlices - x
if y < 0:
return []
return [x, y]
class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
// Let x = jumbo, y = small
// 4x + 2y = tomatoSlices
// x + y = cheeseSlices
// x = (tomatoSlices - 2*cheeseSlices)/2
int x = tomatoSlices - 2 * cheeseSlices;
if (x < 0 || x % 2 != 0) return {};
x /= 2;
int y = cheeseSlices - x;
if (y < 0) return {};
return {x, y};
}
};
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
// Let x = jumbo, y = small
// 4x + 2y = tomatoSlices
// x + y = cheeseSlices
int x = tomatoSlices - 2 * cheeseSlices;
if (x < 0 || x % 2 != 0) return new ArrayList<>();
x /= 2;
int y = cheeseSlices - x;
if (y < 0) return new ArrayList<>();
return Arrays.asList(x, y);
}
}
var numOfBurgers = function(tomatoSlices, cheeseSlices) {
// Let x = jumbo, y = small
// 4x + 2y = tomatoSlices
// x + y = cheeseSlices
let x = tomatoSlices - 2 * cheeseSlices;
if (x < 0 || x % 2 !== 0) return [];
x = Math.floor(x / 2);
let y = cheeseSlices - x;
if (y < 0) return [];
return [x, y];
};
tomatoSlices and cheeseSlices.
There are only two types of burgers you can make:
x be the number of Jumbo burgers and y the number of Small burgers. Then:
4x + 2y = tomatoSlicesx + y = cheeseSlicesx + y = cheeseSlices, y = cheeseSlices - x.4x + 2y = tomatoSlicesy: 4x + 2(cheeseSlices - x) = tomatoSlices4x + 2cheeseSlices - 2x = tomatoSlices2x = tomatoSlices - 2cheeseSlicesx = (tomatoSlices - 2 * cheeseSlices) / 2y = cheeseSlices - xx and y must be integers, so tomatoSlices - 2 * cheeseSlices must be even.x and y must be non-negative.[x, y], otherwise return an empty list.x = (16 - 2*7)/2 = (16 - 14)/2 = 1y = 7 - 1 = 6x and y non-negative? Yes.4*1 + 2*6 = 4 + 12 = 16 (matches tomatoSlices)1 + 6 = 7 (matches cheeseSlices)[1, 6]: 1 Jumbo and 6 Small burgers.cheeseSlices, so time complexity is O(N), where N is cheeseSlices.