📌  相关文章
📜  检查是否可以通过从任何一个袋子中取出 2 个糖果和从另外两个袋子中反复取出 1 个来清空所有 3 个糖果袋

📅  最后修改于: 2021-09-24 03:18:07             🧑  作者: Mango

给定 3 个整数a、bc,表示三个袋子中的糖果数量。您需要通过执行特定操作来确定我们是否可以清空所有袋子,其中在每个操作中,您可以从一个袋子中吃2 个糖果,从其他 2 个袋子中1 个糖果。不允许跳过任何袋子,即每次操作必须从每个袋子中吃掉 1 或 2 个糖果。如果可能,返回真,否则返回假。

例子:

朴素的方法:遍历变量直到所有三个都不是 0。在每次迭代中,最大元素减少 2,剩余变量减少 1。

时间复杂度: O(N),其中 N 是a、bc的最大变量值

有效方法:通过进行以下观察,可以使用有效方法解决给定的问题:

  • 在每个操作中,我们将选择 1+2+1=4 个糖果,因此 bag1、bag2 和 bag3中所有糖果总和必须能被 4 整除
  • 清空所有袋子所需的操作次数为 (所有糖果的总和)/4 .
  • 我们必须在任何操作中从一个袋子中挑选 1 或 2 个糖果,因此3 个袋子中最小糖果必须大于或等于操作次数。
C++
// C++ code for the above approach
 
#include 
#define ll long long
using namespace std;
 
bool can_empty(ll a, ll b, ll c)
{
    // If total candies are not multiple
    // of 4 then its not possible to be
    // left with 0 candies
    if ((a + b + c) % 4 != 0)
        return false;
    else {
 
        // If minimum candies of three bags
        // are less than number of operations
        // required then the task is not possible
        int m = min(a, min(b, c));
        if (m < ((a + b + c) / 4))
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    ll a = 4, b = 2, c = 2;
    cout << (can_empty(a, b, c) ? "true" : "false")
         << endl;
 
    a = 3, b = 4, c = 2;
    cout << (can_empty(a, b, c) ? "true" : "false")
         << endl;
 
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
 
static boolean can_empty(int a, int b, int c)
{
     
    // If total candies are not multiple
    // of 4 then its not possible to be
    // left with 0 candies
    if ((a + b + c) % 4 != 0)
        return false;
    else
    {
         
        // If minimum candies of three bags
        // are less than number of operations
        // required then the task is not possible
        int m = Math.min(a, Math.min(b, c));
        if (m < ((a + b + c) / 4))
            return false;
    }
    return true;
}
   
// Driver Code
public static void main(String[] args)
{
    int a = 4, b = 2, c = 2;
    System.out.println(can_empty(a, b, c) ?
                       "true" : "false");
 
    a = 3;
    b = 4;
    c = 2;
    System.out.println(can_empty(a, b, c) ?
                       "true" : "false");
}
}
 
// This code is contributed by code_hunt


Python3
# Python code for the above approach
def can_empty(a, b, c):
     
    # If total candies are not multiple
    # of 4 then its not possible to be
    # left with 0 candies
    if ((a + b + c) % 4 != 0) :
        return False;
    else :
         
        # If minimum candies of three bags
        # are less than number of operations
        # required then the task is not possible
        m = min(a, min(b, c));
        if (m < (a + b + c) // 4) :
            return False;
     
    return True;
 
# Driver code
a = 4
b = 2
c = 2
print("true" if can_empty(a, b, c) else "false");
 
a = 3
b = 4
c = 2
print("true" if can_empty(a, b, c) else "false");
 
# This code is contributed by _saurabh_jaiswal


Javascript


输出:
true
false

时间复杂度: O(1)
空间复杂度: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程