📌  相关文章
📜  计算消耗糖果的最大数量

📅  最后修改于: 2021-09-06 06:48:03             🧑  作者: Mango

给定两个数组A[]B[]N 个整数组成,分别代表每种糖果的数量和最大消耗限制,以及一个整数M代表添加的未知糖果数量,任务是找到最大数量一个人可以蒙着眼睛吃的糖果。

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:

  1. 初始化两个变量,例如anstotal ,以存储可安全食用的最大糖果数量和糖果总数
  2. 初始化一个变量,比如allSafe = true ,以检查所有类型的糖果是否都可以安全食用。
  3. 遍历范围[0, N – 1]并且如果A[i] + M > B[i] ,则设置allSafe = false并更新ans = min(ans, B[i]) 。否则,更新ans = min(ans, A[i])。
  4. 如果allSafe为真,则打印数组A[] 的总和。
  5. 否则,将结果打印在ans 中

下面是上述方法的实现:

C++
// C++ implememtation
// of the above approach
#include 
using namespace std;
 
// Function to find the count of
// maximum consumable candies
int maximumCandy(int candies[],
                 int safety[],
                 int N, int M)
{
 
    // Store the count of total candies
    int total = 0;
 
    // Stores the count of maximum
    // consumable candies
    int ans = INT_MAX;
 
    // Checks if it is safe
    // to counsume all candies
    bool all_safe = true;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If A[i] + M is greater than B[i]
        if (candies[i] + M > safety[i]) {
 
            // Mark all_safe as false
            all_safe = false;
 
            // Update ans
            ans = min(ans, safety[i]);
        }
        else {
 
            // Update ans
            ans = min(ans, candies[i] + M);
        }
 
        // Increment total by A[i]
        total += candies[i];
    }
 
    // If all_safe is true
    if (all_safe)
        return total;
 
    // Otherwise,
    else
        return ans;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 4, 1, 9, 6 };
    int B[] = { 8, 7, 3, 12, 7 };
    int M = 0;
 
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function call to find
    // maximum consumable candies
    cout << maximumCandy(A, B, N, M);
 
    return 0;
}


Java
// Java implememtation
// of the above approach
public class GFG
{
 
  // Function to find the count of
  // maximum consumable candies
  static int maximumCandy(int []candies,
                          int []safety,
                          int N, int M)
  {
 
    // Store the count of total candies
    int total = 0;
 
    // Stores the count of maximum
    // consumable candies
    int ans = Integer.MAX_VALUE;
 
    // Checks if it is safe
    // to counsume all candies
    boolean all_safe = true;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // If A[i] + M is greater than B[i]
      if (candies[i] + M > safety[i])
      {
 
        // Mark all_safe as false
        all_safe = false;
 
        // Update ans
        ans = Math.min(ans, safety[i]);
      }
      else
      {
 
        // Update ans
        ans = Math.min(ans, candies[i] + M);
      }
 
      // Increment total by A[i]
      total += candies[i];
    }
 
    // If all_safe is true
    if (all_safe)
      return total;
 
    // Otherwise,
    else
      return ans;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int A[] = { 4, 5, 2, 3 };
    int B[] = { 8, 13, 6, 4 };
    int M = 5;
 
    int N = A.length;
 
    // Function call to find
    // maximum consumable candies
    System.out.println(maximumCandy(A, B, N, M));
 
  }
 
}
 
// This code is contributed by AnkThon


Python3
# Python3 implememtation
# of the above approach
 
# Function to find the count of
# maximum consumable candies
def maximumCandy(candies, safety, N, M):
 
    # Store the count of total candies
    total = 0
 
    # Stores the count of maximum
    # consumable candies
    ans = 10**8
 
    # Checks if it is safe
    # to counsume all candies
    all_safe = True
 
    # Traverse the array arr
    for i in range(N):
 
        # If A[i] + M is greater than B[i]
        if (candies[i] + M > safety[i]):
 
            # Mark all_safe as false
            all_safe = False
 
            # Update ans
            ans = min(ans, safety[i])
        else:
 
            # Update ans
            ans = min(ans, candies[i] + M)
 
        # Increment total by A[i]
        total += candies[i]
 
    # If all_safe is true
    if (all_safe):
        return total
 
    # Otherwise,
    else:
        return ans
 
# Driver Code
if __name__ == '__main__':
    A = [4, 5, 2, 3]
    B = [ 8, 13, 6, 4]
    M = 5
 
    N = len(A)
 
    # Function call to find
    # maximum consumable candies
    print (maximumCandy(A, B, N, M))
 
    # This code is contributed by mohit kumar 29.


C#
// C# implememtation
// of the above approach
using System;
class GFG {
     
    // Function to find the count of
    // maximum consumable candies
    static int maximumCandy(int[] candies, int[] safety, int N, int M)
    {
      
        // Store the count of total candies
        int total = 0;
      
        // Stores the count of maximum
        // consumable candies
        int ans = Int32.MaxValue;
      
        // Checks if it is safe
        // to counsume all candies
        bool all_safe = true;
      
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
      
            // If A[i] + M is greater than B[i]
            if (candies[i] + M > safety[i]) {
      
                // Mark all_safe as false
                all_safe = false;
      
                // Update ans
                ans = Math.Min(ans, safety[i]);
            }
            else {
      
                // Update ans
                ans = Math.Min(ans, candies[i] + M);
            }
      
            // Increment total by A[i]
            total += candies[i];
        }
      
        // If all_safe is true
        if (all_safe)
            return total;
      
        // Otherwise,
        else
            return ans;
    }
 
  // Driver code
  static void Main()
  {
    int[] A = { 4, 5, 2, 3 };
    int[] B = { 8, 13, 6, 4 };
    int M = 5;
  
    int N = A.Length;
  
    // Function call to find
    // maximum consumable candies
    Console.WriteLine(maximumCandy(A, B, N, M));
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live