前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1276. 不浪费原料的汉堡制作方案(解方程)

LeetCode 1276. 不浪费原料的汉堡制作方案(解方程)

作者头像
Michael阿明
发布2020-07-13 15:01:34
8690
发布2020-07-13 15:01:34
举报
文章被收录于专栏:Michael阿明学习之路

1. 题目

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

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

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

请你以 [total_jumbo, total_small]([巨无霸汉堡总数,小皇堡总数])的格式返回恰当的制作方案,使得剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量都是 0。

如果无法使剩下的番茄片 tomatoSlices 和奶酪片 cheeseSlices 的数量为 0,就请返回 []。

代码语言:javascript
复制
示例 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

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/number-of-burgers-with-no-waste-of-ingredients 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

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

4x+2y = tomatoSlices
x+y= cheeseSlices
x = (tomatoSlices - 2*cheeseSlices)/2
代码语言:javascript
复制
class Solution {	//C++
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        int a = tomatoSlices - 2*cheeseSlices;
        if(a < 0 || (a&1)) return {};
        int juwuba = a/2;
        int xiaohuangbao = cheeseSlices - juwuba;
        if(xiaohuangbao >= 0)
            return {juwuba, xiaohuangbao};
        return {};
    }
};

8 ms 7.5 MB

代码语言:javascript
复制
class Solution:	# py3
    def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
        a = tomatoSlices - 2*cheeseSlices
        if a < 0 or a%2==1:
            return []
        juwuba = a//2
        xiaohuangbao = cheeseSlices - juwuba
        if xiaohuangbao >= 0:
            return [juwuba, xiaohuangbao]
        return []

48 ms 13.6 MB

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档