zoukankan      html  css  js  c++  java
  • 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 {
        public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
            List<Integer> res = new ArrayList();
            if(tomatoSlices == 0 && cheeseSlices == 0){
                res.add(0);
                res.add(0);
                 return res;
            }
           
            int half = cheeseSlices / 2;
            //int[][] arr = new int[half + 1][2];
            for(int i = 0; i <= half; i++){
                int t1 =  i;
                int t2 = cheeseSlices - i;
                if(t1 * 4 + t2 * 2 == tomatoSlices){
                    res.add(t1);
                    res.add(t2);
                }
                else if(t1 * 2 + t2 * 4 == tomatoSlices){
                    res.add(t2);
                    res.add(t1);
                }
            }
    
            return res;
        }
    }

    CNM, O(n)都给TLE了

    看了下解答,竟然是鸡兔同笼问题,一想确实有道理

    class Solution {
        public List<Integer> numOfBurgers(int t, int c) {
            List<Integer> res = new ArrayList();
            if(t % 2 == 0 && c * 2 <= t && t <= c * 4){
                res.add(t/2 - c);
                res.add(c * 2 - t / 2);
            }
            else return res;
            return res;
            
        }
    }

     https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/441342/JavaPython-3-Solve-a-linear-equations-group-w-brief-explanation-and-analysis.

  • 相关阅读:
    Hadoop HDFS的常用命令
    Spark简介
    Hadoop datanode无法启动的错误
    kafka在虚拟机环境的优化
    kafka的安装和使用
    Strom的安装及使用
    机器学习
    Tomcat启动时为什么要配置CATALINA_HOME环境变量??
    sqoop的安装和使用
    Python Lambda 的简单用法
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11980598.html
Copyright © 2011-2022 走看看