✅【LeetCode】1276. 不浪费原料的汉堡制作方案

31 阅读1分钟

题目链接

image.png

image.png

image.png

Python3 O(1)\lgroup O(1) \rgroup

class Solution:
    def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
        # total_jumbo = (tomatoSlices - 2*cheeseSlices)//2
        temp = tomatoSlices - 2 * cheeseSlices
        if temp < 0 or temp % 2 != 0:
            return []
        else:
            total_jumbo = temp//2
            total_small = cheeseSlices - total_jumbo
            if total_small < 0:
                return []
            return [total_jumbo, total_small]

        # 4 * total_jumbo + 2 * total_small = tomatoSlices
        # total_jumbo + total_small = cheeseSlices

C++

class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        int temp = tomatoSlices - 2 * cheeseSlices;
        if (temp < 0 || temp % 2 != 0){
            return {};
        }
        else{
            int total_jumbo = temp / 2;
            int total_small = cheeseSlices - total_jumbo;
            if (total_small < 0){
                return {};
            }
            return {total_jumbo, total_small};
        }
    }
};