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;
}
相关推荐
非优秀程序员14 小时前
第05章 播放器大战,暴风、快播与那一夜|《互联网产品恩仇录》
算法
沐晓时光15 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
文丑颜不良啊17 小时前
LeetCode2481~2500题目解析
java·leetcode
Zwawa19 小时前
C语言函数指针与宏实战:从回调、表驱动菜单到X-Macro,讲透嵌入式代码解耦
c语言·单片机·嵌入式
武子康21 小时前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
David猪大卫21 小时前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger21 小时前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
文丑颜不良啊1 天前
LeetCode2461~2480题目解析
java·leetcode
安妮细水长流1 天前
ad7124
c语言·单片机