1276. Number of Burgers with No Waste of Ingredients

Medium


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]
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:

  • 0 <= tomatoSlices, cheeseSlices <= 107

class Solution:
    def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
        # Got this formula using equations 4*x+2*y = tomatoSlices , x+y=cheeseSlices
        y = (2*cheeseSlices - tomatoSlices/2)
        x = (cheeseSlices-y)
        if x < 0 or y < 0 or x%1 or y%1:
            return []
        return [int(x),int(y)]
    
    # TLE with test case 1000000 1000000
    def numOfBurgersOld(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
        @lru_cache()
        def recursion(tomatoSlices, cheeseSlices, jumbo, small):
            if tomatoSlices  == 0 and cheeseSlices == 0:
                return [jumbo, small]
            elif tomatoSlices  < 0 or cheeseSlices < 0:
                # print(jumbo, small)
                return []
            r1 = recursion(tomatoSlices-4, cheeseSlices-1, jumbo + 1, small)
            if r1 :
                return r1
            r2 = recursion(tomatoSlices-2, cheeseSlices-1, jumbo, small+1)
            return r2
        result = recursion(tomatoSlices, cheeseSlices, 0, 0)
        return result

Last updated