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

文章目录

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


写在最后

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

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

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

相关推荐
YuforiaCode2 分钟前
第十二届蓝桥杯 2021 C/C++组 空间
c语言·c++·蓝桥杯
YuforiaCode10 分钟前
第十二届蓝桥杯 2021 C/C++组 卡片
c语言·c++·蓝桥杯
努力努力再努力wz2 小时前
【Linux实践系列】:进程间通信:万字详解命名管道实现通信
android·linux·运维·服务器·c++·c
炯哈哈3 小时前
【上位机——MFC】文档
开发语言·c++·mfc·上位机
愚润求学3 小时前
【C++11】可变参数模板
开发语言·c++·笔记·c++11·模板
WW_千谷山4_sch3 小时前
MYOJ_1349:(洛谷P3951)[NOIP 2017 提高组] 小凯的疑惑(数学公式套用,两步搞定代码)
c++·算法
共享家95274 小时前
深入探究C++ 中的stack、queue和deque
c++
How_doyou_do4 小时前
项目实战-贪吃蛇大作战【补档】
c语言·c++·visual studio
Ethon_王4 小时前
走进Qt--信号与槽机制详解与实战
c++·qt
小卡皮巴拉4 小时前
【力扣刷题实战】丢失的数字
c++·算法·leetcode·位运算