• 【leetcode】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]
    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.
    

    Example 4:

    Input: tomatoSlices = 0, cheeseSlices = 0
    Output: [0,0]
    

    Example 5:

    Input: tomatoSlices = 2, cheeseSlices = 1
    Output: [0,1]

    Constraints:

    • 0 <= tomatoSlices <= 10^7
    • 0 <= cheeseSlices <= 10^7

    解题思路:解二元一次方程。

    代码如下:

    class Solution(object):
        def numOfBurgers(self, tomatoSlices, cheeseSlices):
            """
            :type tomatoSlices: int
            :type cheeseSlices: int
            :rtype: List[int]
            """
            if (tomatoSlices - 2*cheeseSlices)%2 != 0 or (4*cheeseSlices - tomatoSlices) % 2 != 0:
                return []
            elif (tomatoSlices - 2*cheeseSlices)/2 < 0 or (4*cheeseSlices - tomatoSlices) / 2 < 0:
                return []
            return [(tomatoSlices - 2*cheeseSlices)/2, (4*cheeseSlices - tomatoSlices) / 2]
  • 相关阅读:
    白话经典算法系列之中的一个 冒泡排序的三种实现
    Spring3.0 AOP 具体解释
    Android中Preference的使用以及监听事件分析
    Java中的匿名内部类
    【剑指offer】二叉树中和为某一值的路径
    Haskell 差点儿无痛苦上手指南
    Oracle Minus 取差集
    Windows Mobile 6.0 SDK和中文模拟器下载
    caffe源代码分析--math_functions.cu代码研究
    代码阅读分析工具Understand 2.0试用
  • 原文地址:https://www.cnblogs.com/seyjs/p/12004510.html
Copyright © 2020-2023  润新知