给定两个数组A []和B [],它们分别由N个代表每种类型的糖果的数量和最大消耗限制的整数以及一个代表所添加的未知糖果的数量的整数M组成,任务是找到一个人可以在眼罩中食用的糖果。
例子:
Input: A[] = { 4, 5, 2, 3 }, B[] = { 8, 13, 6, 4 }, M = 5
Output: 4
Explanation: Directly consume all 3 candies of 4th type and consume one more candy which can be any of type. Therefore, one can only consume total of 4 candies safely.
Input: A[] = { 2, 4, 1, 9, 6 }, B[] = { 8, 7, 3, 12, 7 }, M = 0
Output: 2
Explanation: One can directly consume all candies as all types of candies are within safe limits.
方法:可以根据以下观察结果解决给定问题:
- If for every type of candies, A[i] + M ≤ B[i], then it is safe to consume all available candies.
- Otherwise, one can only consume minimum of min(A[i] + M, B[i]) for all 0 ≤ i < N.
请按照以下步骤解决问题:
- 初始化两个变量,例如ans和total ,以存储可以安全食用的最大糖果数和糖果总数
- 初始化一个变量,例如allSafe = true ,以检查所有类型的糖果是否可以安全食用。
- 遍历范围[0,N – 1],并且如果A [i] + M> B [i] ,则将allSafe = false设置并更新ans = min(ans,B [i]) 。否则,更新ans = min(ans,A [i])。
- 如果allSafe为true,则打印数组A []的总和。
- 否则,将结果打印在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[] = { 4, 5, 2, 3 };
int B[] = { 8, 13, 6, 4 };
int M = 5;
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.
输出:
4
时间复杂度: O(N)
辅助空间: O(1)