LeetCode 每日一题 Day 23 || 简单数学题

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

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

给你两个整数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 个小皇堡需要 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 <= tomatoSlices <= 10^7

0 <= cheeseSlices <= 10^7

和鸡兔同笼相似的数学题,解方程组就行了:

cpp 复制代码
class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        int temp = (tomatoSlices - 2 * cheeseSlices);
        if(temp < 0 || temp % 2 || temp / 2 > cheeseSlices) {
            return {};
        }
        int res = temp / 2;
        return {res,cheeseSlices - res};
    }
};
cpp 复制代码
class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        // 解方程组
    int x = (tomatoSlices - 2 * cheeseSlices) / 2;
    int y = cheeseSlices - x;
    
    // 检查解是否为非负整数
   if(x < 0 || y < 0 || 4 * x + 2 * y != tomatoSlices){
       return {};
   }

   return {x , y};
}
};
相关推荐
weixin_3077791315 分钟前
C++代码实现MATLAB中的ode23t函数功能
开发语言·c++·算法·matlab
萧西待水31 分钟前
奥赛一本通 1451 棋盘游戏
算法·宽度优先
Niuguangshuo39 分钟前
论文解读:Paraformer,非自回归中文 ASR 的并行 Transformer
算法·音视频·语音识别
鹿角片ljp1 小时前
LeetCode 78:子集|回溯、选与不选、递归和path快照
java·数据结构·算法
圣保罗的大教堂1 小时前
leetcode 2033. 获取单值网格的最小操作数 中等
leetcode
hansang_IR1 小时前
【代数与组合数学 | 那忘算 5】生成函数 & 例题 & 卷积
c++·算法·多项式·生成函数·母函数
Zane19941 小时前
快速排序凭什么叫"快"排序?平均O(nlogn)背后,藏着一个能让它退化成O(n²)的选择
算法
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 242. 有效的字母异位词 | C++ 哈希计数与严密防线
c++·算法·leetcode
wabs6661 小时前
关于二叉树【力扣101.对称二叉树的思考】
数据结构·c++·算法·leetcode·二叉树
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 228. 汇总区间 | C++ 双游标区间扫描与 to_string 规范
c++·算法·leetcode