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;
}
相关推荐
·present·7 小时前
CSAPP(lecture 4_01)
c语言·c++
秋天的怀念_David8 小时前
算法一千零一夜 · 第十四夜 幻方与奇妙的二进制
java·程序人生·算法
薛定e的猫咪8 小时前
(ICLR 2020)MAML 靠的不是快速学习,而是特征复用
人工智能·深度学习·学习·算法·机器学习
薛定e的猫咪8 小时前
(ICLR 2026)DF-META:面向 FJSP 的无导数元学习实例自适应调度
人工智能·深度学习·学习·算法·机器学习
java_nnnn10 小时前
LeetCode刷题滑动窗口(水果成篮+找到字符串中所有字母的异位词)Java
java·开发语言·leetcode
小羊没烦恼!10 小时前
异步编程:线程概述及使用
java·开发语言·mysql·算法·c#
wanderist.10 小时前
202609CSP认证P3
算法
云淡风轻~窗明几净11 小时前
角谷猜想最接近严格证明的测试 Version1.1.1
人工智能·算法·机器学习·图论
机器学习之心11 小时前
PINN × SVM × NSGA-II × 熵权TOPSIS:一套跑通「建模—优化—决策」的多目标优化框架
算法·机器学习·支持向量机
本地化文档11 小时前
llvm-libc-docs-l10n
c语言·github·gitcode·sphinx·llvm·crowdin