LeetCode-in-Java

1276. Number of Burgers with No Waste of Ingredients

Medium

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

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] Explantion: To make one jumbo burger and 6 small burgers we need 4*1 + 2*6 = 16 tomato and 1 + 6 = 7 cheese. There will be no remaining ingredients.

Example 2:

Input: tomatoSlices = 17, cheeseSlices = 4

Output: [] Explantion: There will be no way to use all ingredients to make small and jumbo burgers.

Example 3:

Input: tomatoSlices = 4, cheeseSlices = 17

Output: [] Explantion: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.

Constraints:

Solution

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        List<Integer> numbers = new ArrayList<>();
        int numberOfCheese = cheeseSlices * 4;
        int remaining = numberOfCheese - tomatoSlices;
        if (remaining >= 0 && remaining % 2 != 1) {
            int numberOfSmall = remaining / 2;
            int numberOfLarge = cheeseSlices - numberOfSmall;
            if (numberOfLarge < 0) {
                return numbers;
            }
            numbers.add(numberOfLarge);
            numbers.add(numberOfSmall);
        }
        return numbers;
    }
}