同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
圣诞活动预热开始啦,汉堡店推出了全新的汉堡套餐。为了避免浪费原料,请你帮他们制定合适的制作计划。
给你两个整数 tomatoSlices
和 cheeseSlices
,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:
请你以 [total_jumbo, total_small]
([巨无霸汉堡总数,小皇堡总数])的格式返回恰当的制作方案,使得剩下的番茄片 tomatoSlices
和奶酪片 cheeseSlices
的数量都是 0
。
如果无法使剩下的番茄片 tomatoSlices
和奶酪片 cheeseSlices
的数量为 0
,就请返回 []
。
示例 1:
输入:tomatoSlices = 16, cheeseSlices = 7 输出:[1,6] 解释:制作 1 个巨无霸汉堡和 6 个小皇堡需要 4*1 + 2*6 = 16 片番茄和 1 + 6 = 7 片奶酪。不会剩下原料。
示例 2:
输入:tomatoSlices = 17, cheeseSlices = 4 输出:[] 解释:只制作小皇堡和巨无霸汉堡无法用光全部原料。
示例 3:
输入:tomatoSlices = 4, cheeseSlices = 17 输出:[] 解释:制作 1 个巨无霸汉堡会剩下 16 片奶酪,制作 2 个小皇堡会剩下 15 片奶酪。
示例 4:
输入:tomatoSlices = 0, cheeseSlices = 0 输出:[0,0]
示例 5:
输入:tomatoSlices = 2, cheeseSlices = 1 输出:[0,1]
提示:
0 <= tomatoSlices <= 10^7
0 <= cheeseSlices <= 10^7
我们设巨无霸汉堡数量为 $x$,小皇堡数量为 $y$,则有:
$$ \begin{aligned} 4x + 2y &= tomatoSlices \\ x + y &= cheeseSlices \end{aligned} $$
将上述两式转换,可以得到:
$$ \begin{aligned} y = (4 \times cheeseSlices - tomatoSlices) / 2 \\ x = cheeseSlices - y \end{aligned} $$
其中 $x$ 和 $y$ 必须为非负整数。
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
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]
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
}
}
class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 || x < 0 || y < 0 ? vector<int>{} : vector<int>{x, y};
}
};
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}
}
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
const k = 4 * cheeseSlices - tomatoSlices;
const y = k >> 1;
const x = cheeseSlices - y;
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
}
impl Solution {
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
let k = 4 * cheese_slices - tomato_slices;
let y = k / 2;
let x = cheese_slices - y;
if k % 2 != 0 || y < 0 || x < 0 {
Vec::new()
} else {
vec![x, y]
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。