不浪费原料的汉堡制作方案(LeetCode日记)

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

题目信息:

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

给你两个整数 t o m a t o S l i c e s tomatoSlices tomatoSlices c h e e s e S l i c e s cheeseSlices cheeseSlices,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:

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

请你以 [ t o t a l j u m b o , t o t a l s m a l l ] [total_jumbo, total_small] [totaljumbo,totalsmall]([巨无霸汉堡总数,小皇堡总数])的格式返回恰当的制作方案,使得剩下的番茄片 t o m a t o S l i c e s tomatoSlices tomatoSlices 和奶酪片 c h e e s e S l i c e s cheeseSlices cheeseSlices 的数量都是 0。

如果无法使剩下的番茄片 t o m a t o S l i c e s tomatoSlices tomatoSlices 和奶酪片 c h e e s e S l i c e s cheeseSlices cheeseSlices 的数量为 0,就请返回 [ ] [ ] []

  • 示例1:

输入:tomatoSlices = 16, cheeseSlices = 7
输出:[1,6]
解释:制作 1 个巨无霸汉堡和 6 个小皇堡需要 41 + 26 = 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 < = t o m a t o S l i c e s < = 1 0 7 0 <= tomatoSlices <= 10^7 0<=tomatoSlices<=107
  • 0 < = c h e e s e S l i c e s < = 1 0 7 0 <= cheeseSlices <= 10^7 0<=cheeseSlices<=107

相关标签 :数学

题解

今天的问题是一个典型的鸡兔同笼问题,还是相对简单的。

方法:数学

设巨无霸汉堡有 xxx 个,皇堡有 yyy 个,由于所有的材料都需要用完,因此我们可以得到二元一次方程组:

{ 4 x + 2 y =  tomatoSlices  x + y =  cheeseSlices  \left\{\begin{array}{l} 4 x+2 y=\text { tomatoSlices } \\ x+y=\text { cheeseSlices } \end{array}\right. { 4x+2y= tomatoSlices x+y= cheeseSlices 

根据题意, x , y ≥ 0 x , y ≥ 0 x , y ≥ 0 且 x , y ∈ N x , y ∈ N x , y ∈ N x,y≥0x, y \geq 0x,y≥0 且 x,y∈Nx, y \in \mathbb{N}x,y∈N x,y0x,y0x,y0x,yNx,yNx,yN ,解方程,得:
{  tomatoSlices  = 2 k , k ∈ N  tomatoSlices  ≥ 2 ×  cheeseSlices  4 ×  cheeseSlices  ≥  tomatoSlices  \left\{\begin{array}{l} \text { tomatoSlices }=2 k, \quad k \in \mathbb{N} \\ \text { tomatoSlices } \geq 2 \times \text { cheeseSlices } \\ 4 \times \text { cheeseSlices } \geq \text { tomatoSlices } \end{array}\right.  tomatoSlices =2k,kN tomatoSlices 2× cheeseSlices 4× cheeseSlices  tomatoSlices 

实现代码(Python)
class Solution:
    def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
        if tomatoSlices % 2 != 0 \
        or tomatoSlices < cheeseSlices * 2 \
        or cheeseSlices * 4 < tomatoSlices:
            return []
        return [tomatoSlices // 2 - cheeseSlices, \
        cheeseSlices * 2 - tomatoSlices // 2]
复杂度分析:
  • 时间复杂度: O ( 1 ) O(1) O(1)
  • 空间复杂度: O ( 1 ) O(1) O(1)

题记:


  • 研究生在读,我会尽量保持LeetCode每日一题的思路和代码输出。希望大家多多支持。
  • 水平有限,希望各位大佬能够批评指正。您的教诲是我进步的船帆。
  • 希望各位跟我一样的小白能跟我一起参与到做题和讨论中来。共同进步是我所能期盼的最高愿想。
  • 您的点赞和关注是我坚持分享的动力泉源,希望能将这件简单平凡的事一直做下去。感谢大家。

相关推荐

  1. 浪费原料汉堡制作方案(LeetCode日记)

    2023-12-29 06:00:05       34 阅读
  2. LeetCode——1276. 浪费原料汉堡制作方案

    2023-12-29 06:00:05       36 阅读
  3. LeetCode解法汇总1276. 浪费原料汉堡制作方案

    2023-12-29 06:00:05       39 阅读
  4. 保龄球游戏获胜者(LeetCode日记)

    2023-12-29 06:00:05       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 06:00:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 06:00:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 06:00:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 06:00:05       18 阅读

热门阅读

  1. Leetcode-230.二叉搜索树中第k小的元素(Python)

    2023-12-29 06:00:05       40 阅读
  2. Zeppelin安装教程

    2023-12-29 06:00:05       33 阅读
  3. 使用pandas绘图,并保存,支持中文

    2023-12-29 06:00:05       30 阅读
  4. 07.kubernetes客户端部署

    2023-12-29 06:00:05       38 阅读
  5. oracle linux 8升级gcc gcc9

    2023-12-29 06:00:05       39 阅读
  6. Linux基础命令之系统管理常用命令

    2023-12-29 06:00:05       34 阅读
  7. pfc001 Not enough information

    2023-12-29 06:00:05       29 阅读
  8. trino-435:dynamic catalog

    2023-12-29 06:00:05       37 阅读
  9. (js)循环判断找到满足条件的单项后结束循环

    2023-12-29 06:00:05       34 阅读