最大化可以由给定类型和百搭牌组成的牌组数量
给定一个整数N表示特定牌组中卡片的数量和一个大小为N的数组arr[] ,其中第i个元素表示第i种卡片的频率。还给了K Jokers。任务是找到具有给定数据的最大可能有效牌组。
有效的套牌应遵循上述两个条件之一:
1.一副牌,每种牌只包含一张牌,没有小丑。
2.一副牌,每种类型的牌都只有一张,除了一张,还有一张小丑。
例子:
Input: N = 2, arr[] = {10, 15}, K = 3
Output: 13
Explanation: Below are the ways in which 13 decks are made:
10 decks of type {1, 2}
3 decks of type {J, 2}
Therefore maximum possible number of decks are 10 + 3 = 13 decks.
Input: N = 3, arr[] = {1, 2, 3}, K = 4
Output: 3
Explanation: Below are the ways in which 13 decks are made:
1 deck of type {1, 2, 3}
1 deck of type {J, 2, 3}
1 deck of type {J, 2, 3}
Therefore maximum possible number of decks are 1+1+1 = 3 decks.
方法:给定的问题可以通过使用贪心方法和二分搜索算法来解决。请按照以下步骤解决给定的问题。
- 以非递减顺序对给定数组arr[]进行排序。
- 可以对答案应用二进制搜索以获得最大值。
- 初始化两个变量,例如L = 0和R = max_element(arr) + K + 1 ,这将定义要找出的答案的初始范围。
- 在L +1 < R时迭代
- 初始化一个变量mid = (L + R)/2以在每次迭代时跟踪范围内的中间元素。
- 用一个 变量说, need = 0来计算当前中间所需的额外卡片 元素。
- 使用变量i迭代整个数组arr[] :
- 如果arr[i] < 中间集需要 = 需要 + arr[i] – 中间。
- 如果 需要 <= mid和需要 <= k ,设置L = mid 。
- 否则设置R = mid 。
- 最后最终答案将存储在L中,因此返回L作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum possible decks
// with given frequency of cards and
// number of jokers
int maximumCardDecks(int arr[], int n, int k)
{
// Sort the whole array
sort(arr, arr + n);
// Store the binary search range
int L = 0;
int R = arr[n - 1] + k + 1;
// Do a binary search on range
while (L + 1 < R) {
// Compute the mid value of the current
// binary search range
int mid = (L + R) / 2;
// Store extra needed
int need = 0;
// Iterate over the total cards and
// compute variable need.
for (int i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
// Driver Code
int main()
{
int N = 3, K = 4;
int arr[] = { 1, 2, 3 };
cout << maximumCardDecks(arr, N, K);
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to find maximum possible decks
// with given frequency of cards and
// number of jokers
static int maximumCardDecks(int arr[], int n, int k)
{
// Sort the whole array
Arrays.sort(arr);
// Store the binary search range
int L = 0;
int R = arr[n - 1] + k + 1;
// Do a binary search on range
while (L + 1 < R)
{
// Compute the mid value of the current
// binary search range
int mid = (L + R) / 2;
// Store extra needed
int need = 0;
// Iterate over the total cards and
// compute variable need.
for (int i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
// Driver Code
public static void main (String[] args)
{
int N = 3, K = 4;
int arr[] = { 1, 2, 3 };
System.out.print(maximumCardDecks(arr, N, K));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python Program to implement
# the above approach
# Function to find maximum possible decks
# with given frequency of cards and
# number of jokers
def maximumCardDecks(arr, n, k):
# Sort the whole array
arr.sort()
# Store the binary search range
L = 0
R = arr[n - 1] + k + 1
# Do a binary search on range
while (L + 1 < R) :
# Compute the mid value of the current
# binary search range
mid = (L + R) // 2
# Store extra needed
need = 0
# Iterate over the total cards and
# compute variable need.
for i in range(n):
if (arr[i] < mid):
need += mid - arr[i]
if (need <= mid and need <= k):
L = mid
else:
R = mid
return L
# Driver Code
N = 3
K = 4
arr = [1, 2, 3]
print(maximumCardDecks(arr, N, K))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find maximum possible decks
// with given frequency of cards and
// number of jokers
static int maximumCardDecks(int []arr, int n, int k)
{
// Sort the whole array
Array.Sort(arr);
// Store the binary search range
int L = 0;
int R = arr[n - 1] + k + 1;
// Do a binary search on range
while (L + 1 < R)
{
// Compute the mid value of the current
// binary search range
int mid = (L + R) / 2;
// Store extra needed
int need = 0;
// Iterate over the total cards and
// compute variable need.
for (int i = 0; i < n; i++) {
if (arr[i] < mid) {
need += mid - arr[i];
}
}
if (need <= mid && need <= k) {
L = mid;
}
else {
R = mid;
}
}
return L;
}
// Driver Code
public static void Main (String[] args)
{
int N = 3, K = 4;
int []arr = { 1, 2, 3 };
Console.Write(maximumCardDecks(arr, N, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
3
时间复杂度:O(N(log(N) + log(M + K)),其中 M 是数组的最大元素。
辅助空间: O(1)