LeetCode 题解工作台

不浪费原料的汉堡制作方案

圣诞活动预热开始啦,汉堡店推出了全新的汉堡套餐。为了避免浪费原料,请你帮他们制定合适的制作计划。 给你两个整数 tomatoSlices 和 cheeseSlices ,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下: 巨无霸汉堡: 4 片番茄和 1 片奶酪 小皇堡: 2 片番茄和 1 片奶酪…

category

1

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·driven

bolt

答案摘要

我们设巨无霸汉堡数量为 ,小皇堡数量为 ,则有: $$

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·driven 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

圣诞活动预热开始啦,汉堡店推出了全新的汉堡套餐。为了避免浪费原料,请你帮他们制定合适的制作计划。

给你两个整数 tomatoSlices 和 cheeseSlices,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:

  • 巨无霸汉堡:4 片番茄和 1 片奶酪
  • 小皇堡:2 片番茄和 1 片奶酪

请你以 [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
lightbulb

解题思路

方法一:数学

我们设巨无霸汉堡数量为 xx,小皇堡数量为 yy,则有:

4x+2y=tomatoSlicesx+y=cheeseSlices\begin{aligned} 4x + 2y &= tomatoSlices \\ x + y &= cheeseSlices \end{aligned}

将上述两式转换,可以得到:

y=(4×cheeseSlicestomatoSlices)/2x=cheeseSlicesy\begin{aligned} y = (4 \times cheeseSlices - tomatoSlices) / 2 \\ x = cheeseSlices - y \end{aligned}

其中 xxyy 必须为非负整数。

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
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]
speed

复杂度分析

指标
时间complexity is O(1) because calculations involve direct arithmetic without iteration. Space complexity is O(1) since only a few integer variables are stored, regardless of input size.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for direct algebraic formulation rather than iterative brute force.

  • question_mark

    Check candidate solutions against ingredient parity and zero-remainder constraints.

  • question_mark

    Consider edge cases where tomatoSlices or cheeseSlices are zero or insufficient.

warning

常见陷阱

外企场景
  • error

    Failing to check that tomatoSlices must be even for a valid solution.

  • error

    Assuming negative counts are valid for jumbo or small burgers.

  • error

    Ignoring integer division issues leading to fractional burger counts.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Varying burger ingredient ratios, e.g., jumbo uses 5 tomato slices instead of 4.

  • arrow_right_alt

    Limiting total number of burgers available, requiring optimization within constraints.

  • arrow_right_alt

    Adding multiple burger types beyond jumbo and small with unique ingredient requirements.

help

常见问题

外企场景

不浪费原料的汉堡制作方案题解:数学·driven | LeetCode #1276 中等