JZX轻语:简
LeetCode 1276 - 不浪费原料的汉堡制作方案
发表于2024年07月16日
鸡兔同笼问题的变种,给定两个参数tomatoSlices(简记为t)和cheeseSlices(简记为c)表示番茄和奶酪的数量,问在用尽所有原料的情况下,能制作多少个巨无霸汉堡(记为x)和小皇堡(记为y)。不难得知x + y = c,4x + 2y = t,解方程组即可得到结果x = t / 2 - c,y = 2c - t / 2。需要注意的是,x和y必须是整数,且x >= 0,y >= 0,则有t必须是偶数,且2c <= t <= 4c。我们先判断t是否是偶数,然后判断t是否在合理范围内,如果满足条件,则返回[x, y],否则返回[]。
class Solution:
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
if tomatoSlices % 2 or not (2 * cheeseSlices <= tomatoSlices <= 4 * cheeseSlices):
return []
return [tomatoSlices // 2 - cheeseSlices, 2 * cheeseSlices - tomatoSlices // 2]