【每日一题】不浪费原料的汉堡制作方案

文章目录

Tag

【解方程】【2023-12-25】


题目来源

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


解题思路

方法一:解方程

思路

这是一个简单的解决二元一次方程的问题。

根据题意有以下关系式:

{ t o t a l _ j u m b o × 4 + t o t a l _ s m a l l × 2 = t o m a t o S l i c e s t o t a l _ j u m b o + t o t a l _ s m a l l = c h e e s S l i c e s \left\{ \begin{array}{c} total\_jumbo\times 4+\ total\_small\times 2=tomatoSlices\\ total\_jumbo+total\_small=cheesSlices\\ \end{array} \right. {total_jumbo×4+ total_small×2=tomatoSlicestotal_jumbo+total_small=cheesSlices

通过简单的消元可得:

{ t o t a l _ j u m b o = 1 2 × t o m a t o S l i c e s − c h e e s e S l i c e s t o t a l _ s m a l l = 2 × c h e e s e S l i c e s − 1 2 × t o m a t o S l i c e s \left\{ \begin{array}{c} total\_jumbo=\frac{1}{2}\times tomatoSlices-cheeseSlices\\ total\_small=2\times cheeseSlices-\frac{1}{2}\times tomatoSlices\\ \end{array} \right. {total_jumbo=21×tomatoSlices−cheeseSlicestotal_small=2×cheeseSlices−21×tomatoSlices

根据题意, t o t a l _ j u m b o , t o t a l _ s m a l l ≥ 0 total\_jumbo, total\_small≥0 total_jumbo,total_small≥0 且 x , y ∈ N x, y \in \mathbb{N} x,y∈N,因此需要满足:

{ t o m a t o S l i c e s = 2 k , k ∈ N t o m a t o S l i c e s ≥ 2 × c h e e s e S l i c e s 4 × c h e e s e S l i c e s ≥ t o m a t o S l i c e s \left\{ \begin{array}{c} tomatoSlices=2k,\ k\in \mathbb{N}\\ tomatoSlices\ge 2\times cheeseSlices\\ 4\times cheeseSlices\ge tomatoSlices\\ \end{array} \right. ⎩ ⎨ ⎧tomatoSlices=2k, k∈NtomatoSlices≥2×cheeseSlices4×cheeseSlices≥tomatoSlices

若不满足,则无解。

算法

cpp 复制代码
class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        if (tomatoSlices % 2 || tomatoSlices < cheeseSlices * 2 || cheeseSlices * 4 <tomatoSlices) {
            return {};
        }
        return {tomatoSlices / 2 - cheeseSlices, cheeseSlices * 2 - tomatoSlices / 2};
    }
};

复杂度分析

时间复杂度: O ( 1 ) O(1) O(1)。

空间复杂度: O ( 1 ) O(1) O(1)。


写在最后

如果您发现文章有任何错误或者对文章有任何疑问,欢迎私信博主或者在评论区指出 💬💬💬。

如果大家有更优的时间、空间复杂度的方法,欢迎评论区交流。

最后,感谢您的阅读,如果有所收获的话可以给我点一个 👍 哦。

相关推荐
zaim141 分钟前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
学习使我变快乐44 分钟前
C++:const成员
开发语言·c++
一律清风4 小时前
QT-文件创建时间修改器
c++·qt
风清扬_jd4 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
Death2005 小时前
Qt 6 相比 Qt 5 的主要提升与更新
开发语言·c++·qt·交互·数据可视化
麻辣韭菜7 小时前
网络基础 【HTTP】
网络·c++·http
阿史大杯茶7 小时前
Codeforces Round 976 (Div. 2 ABCDE题)视频讲解
数据结构·c++·算法
转调7 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
wdxylb8 小时前
使用C++的OpenSSL 库实现 AES 加密和解密文件
开发语言·c++·算法
CSP126369 小时前
特别节目————集训总结
c++