-
发表于 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]
- LC 题目链接
-