【LeetCode第 165 場周賽】不浪費原料的漢堡製作方案

  • 2019 年 12 月 3 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/shiliang97/article/details/103334095

5276. 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

來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/number-of-burgers-with-no-waste-of-ingredients 著作權歸領扣網路所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

外國版雞兔同籠?

第一種 4個A,一個B

第二種 2個A,一個B

假設全是第二種就要 2B個A

A-2B就是多出來的,(A-2B)/2就是第一種個數

A-(A-2B)/2就是第二種個數

如果減法出現了負數說明無解

class Solution {  public:      vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {          vector<int> v;          int a=(tomatoSlices-cheeseSlices*2);          if(a%2==0&&a>=0&&cheeseSlices>=a/2){              a=a/2;              v.push_back(a);              v.push_back(cheeseSlices-a);          }else {              return v;          }return v;        }  };