返回介绍

solution / 1200-1299 / 1276.Number of Burgers with No Waste of Ingredients / README_EN

发布于 2024-06-17 01:03:21 字数 4650 浏览 0 评论 0 收藏 0

1276. Number of Burgers with No Waste of Ingredients

中文文档

Description

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

Solutions

Solution 1: Mathematics

We set the number of Jumbo Burgers as $x$ and the number of Small Burgers as $y$, then we have:

$$ \begin{aligned} 4x + 2y &= tomatoSlices \ x + y &= cheeseSlices \end{aligned} $$

Transforming the above two equations, we can get:

$$ \begin{aligned} y = (4 \times cheeseSlices - tomatoSlices) / 2 \ x = cheeseSlices - y \end{aligned} $$

Where $x$ and $y$ must be non-negative integers.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
    k = 4 * cheeseSlices - tomatoSlices
    y = k // 2
    x = cheeseSlices - y
    return [] if k % 2 or y < 0 or x < 0 else [x, y]
class Solution {
  public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
    int k = 4 * cheeseSlices - tomatoSlices;
    int y = k / 2;
    int x = cheeseSlices - y;
    return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
  }
}
class Solution {
public:
  vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
    int k = 4 * cheeseSlices - tomatoSlices;
    int y = k / 2;
    int x = cheeseSlices - y;
    return k % 2 || x < 0 || y < 0 ? vector<int>{} : vector<int>{x, y};
  }
};
func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
  k := 4*cheeseSlices - tomatoSlices
  y := k / 2
  x := cheeseSlices - y
  if k%2 != 0 || x < 0 || y < 0 {
    return []int{}
  }
  return []int{x, y}
}
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
  const k = 4 * cheeseSlices - tomatoSlices;
  const y = k >> 1;
  const x = cheeseSlices - y;
  return k % 2 || y < 0 || x < 0 ? [] : [x, y];
}
impl Solution {
  pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
    let k = 4 * cheese_slices - tomato_slices;
    let y = k / 2;
    let x = cheese_slices - y;
    if k % 2 != 0 || y < 0 || x < 0 {
      Vec::new()
    } else {
      vec![x, y]
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文