LeetCode //C - 1276. Number of Burgers with No Waste of Ingredients

1276. Number of Burgers with No Waste of Ingredients

Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:

  • Jumbo Burger: 4 tomato slices and 1 cheese slice.
  • Small Burger: 2 Tomato slices and 1 cheese slice.

Return total_jumbo, total_small so that the number of remaining tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return \[\].

Example 1:

Input: tomatoSlices = 16, cheeseSlices = 7

Output: 1,6

Explanation: To make one jumbo burger and 6 small burgers we need 41 + 2 6 = 16 tomato and 1 + 6 = 7 cheese.

There will be no remaining ingredients.

Example 2:

Input: tomatoSlices = 17, cheeseSlices = 4

Output: \[\]

Explanation: There will be no way to use all ingredients to make small and jumbo burgers.

Example 3:

Input: tomatoSlices = 4, cheeseSlices = 17

Output: \[\]

Explanation: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.

Constraints:
  • 0 < = t o m a t o S l i c e s , c h e e s e S l i c e s < = 10 7 0 <= tomatoSlices, cheeseSlices <= 10^7 0<=tomatoSlices,cheeseSlices<=107

From: LeetCode

Link: 1276. Number of Burgers with No Waste of Ingredients


Solution:

Ideas:

Set up two equations (4j + 2s = tomatoSlices and j + s = cheeseSlices), solve for the number of jumbo and small burgers, and return them only if both are non-negative integers; otherwise return an empty array.

Code:
c 复制代码
#include <stdlib.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* numOfBurgers(int tomatoSlices, int cheeseSlices, int* returnSize) {
    *returnSize = 0;

    /* Tomato slices must be even. */
    if (tomatoSlices % 2 != 0) {
        return NULL;
    }

    int jumbo = tomatoSlices / 2 - cheeseSlices;
    int small = 2 * cheeseSlices - tomatoSlices / 2;

    /* Burger counts cannot be negative. */
    if (jumbo < 0 || small < 0) {
        return NULL;
    }

    int* result = (int*)malloc(2 * sizeof(int));

    if (result == NULL) {
        return NULL;
    }

    result[0] = jumbo;
    result[1] = small;
    *returnSize = 2;

    return result;
}
相关推荐
FRing032 小时前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
老歌老听老掉牙2 小时前
三个等价答案:遗传算法如何逼近 Branin 函数
c++·算法·ga
是main不是漫2 小时前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
老花眼猫3 小时前
C语言绘图方法:(24)鼓形缩放旋转
c语言·经验分享·青少年编程·课程设计
慧都产品智能化3 小时前
汽车软件年底自查:按安全影响排定缺陷整改顺序
c语言·c++·测试工具·单元测试·汽车·代码规范
geovindu3 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
Zwawa3 小时前
USB 与有线接口:插上去之后,这条线上发生了什么
c语言·单片机·嵌入式
Navigator_Z4 小时前
LeetCode //C - 1328. Break a Palindrome
c语言·算法·leetcode
电科_银尘4 小时前
【大模型算法与工程 46】vLLM部署入门:模型服务、请求与运行观测
算法·vllm
AskHarries5 小时前
社区运营打法
算法