给定三个整数, C0、C1 和 C2在组S 中的频率为 0、1 和 2。任务是找到总和可被 3 整除的组的最大数量,条件是sum(S)可被 3 整除,并且所有组的并集必须等于S
例子:
Input: C0 = 2, C1 = 4, C2 = 1
Output: 4
Explanation: it can divide the group S = {0, 0, 1, 1, 1, 1, 2} into four groups {0}, {0}, {1, 1, 1}, {1, 2}. It can be proven that 4 is the maximum possible answer.
Input: C0 = 250, C1 = 0, C2 = 0
Output: 250
方法:这个问题可以使用贪心算法来解决。请按照以下步骤解决问题。
- 初始化一个变量maxAns ,比如 0,以存储组的最大数量。
- 将C0添加到 maxAns ,因为每个{0}都可以是一个组,使得总和可以被 3 整除。
- 初始化一个变量k ,比如min(C1, C2) ,并将其添加到 maxAns 中,因为至少可以创建k , {1, 2}组。
- 将abs(C1-C2) /3 添加到 maxAns ,它将贡献剩余的1s 或 2s 。
- 返回maxAns。
下面是上述方法的实现。
C++
// C++ program for above appraoch
#include
using namespace std;
int maxGroup(int c0, int c1, int c2)
{
// Initializing to store maximum number of groups
int maxAns = 0;
// Adding C0
maxAns += c0;
// Taking Minimum of c1, c2 as minimum number of
// pairs must be minimum of c1, c2
int k = min(c1, c2);
maxAns += k;
// If there is any remaining element in c1 or c2
// then it must be the absolute difference of c1 and
// c2 and dividing it by 3 to make it one pair
maxAns += abs(c1 - c2) / 3;
return maxAns;
}
int main()
{
int C0 = 2, C1 = 4, C2 = 1;
cout << maxGroup(C0, C1, C2);
return 0;
}
// This code is contributed by maddler
Java
// Java program for above appraoch
import java.io.*;
class GFG {
// Function to calculate maximum number of groups
public static int maxGroup(int c0, int c1, int c2)
{
// Initializing to store maximum number of groups
int maxAns = 0;
// Adding C0
maxAns += c0;
// Taking Minimum of c1, c2 as minimum number of
// pairs must be minimum of c1, c2
int k = Math.min(c1, c2);
maxAns += k;
// If there is any remaining element in c1 or c2
// then it must be the absolute difference of c1 and
// c2 and dividing it by 3 to make it one pair
maxAns += Math.abs(c1 - c2) / 3;
return maxAns;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int C0 = 2, C1 = 4, C2 = 1;
// Function Call
System.out.println(maxGroup(C0, C1, C2));
}
}
Python3
# python 3 program for above appraoch
def maxGroup(c0, c1, c2):
# Initializing to store maximum number of groups
maxAns = 0
# Adding C0
maxAns += c0
# Taking Minimum of c1, c2 as minimum number of
# pairs must be minimum of c1, c2
k = min(c1, c2)
maxAns += k
# If there is any remaining element in c1 or c2
# then it must be the absolute difference of c1 and
# c2 and dividing it by 3 to make it one pair
maxAns += abs(c1 - c2) // 3
return maxAns
# Driver code
if __name__ == '__main__':
C0 = 2
C1 = 4
C2 = 1
print(maxGroup(C0, C1, C2))
# This code is contributed by ipg2016107.
C#
// C# program for above appraoch
using System;
class GFG{
// Function to calculate maximum number of groups
public static int maxGroup(int c0, int c1, int c2)
{
// Initializing to store maximum number
// of groups
int maxAns = 0;
// Adding C0
maxAns += c0;
// Taking Minimum of c1, c2 as minimum number
// of pairs must be minimum of c1, c2
int k = Math.Min(c1, c2);
maxAns += k;
// If there is any remaining element
// in c1 or c2 then it must be the
// absolute difference of c1 and c2
// and dividing it by 3 to make it one pair
maxAns += Math.Abs(c1 - c2) / 3;
return maxAns;
}
// Driver Code
static public void Main()
{
// Given Input
int C0 = 2, C1 = 4, C2 = 1;
// Function Call
Console.WriteLine(maxGroup(C0, C1, C2));
}
}
// This code is contributed by maddler
Javascript
输出:
4
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。