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

文章目录

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


写在最后

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

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

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

相关推荐
张太行_1 小时前
C++中的析构器(Destructor)(也称为析构函数)
开发语言·c++
涛ing6 小时前
32. C 语言 安全函数( _s 尾缀)
linux·c语言·c++·vscode·算法·安全·vim
独正己身6 小时前
代码随想录day4
数据结构·c++·算法
我不是代码教父9 小时前
[原创](Modern C++)现代C++的关键性概念: 流格式化
c++·字符串格式化·流格式化·cout格式化
利刃大大9 小时前
【回溯+剪枝】找出所有子集的异或总和再求和 && 全排列Ⅱ
c++·算法·深度优先·剪枝
子燕若水9 小时前
mac 手工安装OpenSSL 3.4.0
c++
*TQK*9 小时前
ZZNUOJ(C/C++)基础练习1041——1050(详解版)
c语言·c++·编程知识点
ElseWhereR10 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
*TQK*10 小时前
ZZNUOJ(C/C++)基础练习1031——1040(详解版)
c语言·c++·编程知识点
※DX3906※11 小时前
cpp实战项目—string类的模拟实现
开发语言·c++