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

文章目录

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)。


写在最后

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

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

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

相关推荐
whoarethenext7 分钟前
C++ OpenCV 学习路线图
c++·opencv·学习
闻缺陷则喜何志丹17 分钟前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
hutaotaotao1 小时前
c++中的输入输出流(标准IO,文件IO,字符串IO)
c++·io·fstream·sstream·iostream
AL流云。1 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法
qq_429879672 小时前
省略号和可变参数模板
开发语言·c++·算法
CodeWithMe3 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi3 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
zh_xuan7 小时前
c++ 单例模式
开发语言·c++·单例模式
利刃大大10 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
喜欢吃燃面10 小时前
C++刷题:日期模拟(1)
c++·学习·算法