给定一个由N 个正整数组成的数组arr[] ,其中arr[i]代表硬币的价值,任务是找到当两个玩家A和B玩游戏时每个玩家可以获得的最大金额按照以下规则进行最佳游戏:
- 玩家A总是开始游戏。
- 在每一轮中,玩家必须从给定的数组中取出1 个硬币。
- 在每一轮中,玩家B必须从给定的数组中移除恰好 2 个硬币。
例子:
Input: arr[] = {1, 1, 1, 1}
Output: (A : 2), (B : 2)
Explanation:
Following are the sequences of coins removed for both the players:
- Player A removes arr[0](= 1).
- Player B removes arr[1](= 1) and arr[2](= 1).
- Player A removes arr[3](= 1).
Therefore, the total coins obtained by Player A is 2 and Player B is 2.
Input: arr[] = {1, 1, 1}
Output: (A : 1), (B : 2)
方法:可以使用贪心方法解决给定的问题。请按照以下步骤解决问题:
- 将两个变量(例如amountA和amountB )初始化为0 ,用于存储玩家A和B获得的总金额。
- 按降序对给定数组进行排序。
- 如果N 的值为1 ,则将amountA的值更新为arr[0] 。
- 如果N的值大于或等于2 ,则将amountA的值更新为arr[0] ,将amountB的值更新为arr[1] 。
- 使用变量i在范围[2, N] 上遍历数组arr[] ,如果 i 的值是偶数,则将值arr[i] 添加到amountB 。否则,将值arr[i]添加到amountA 。
- 完成上述步骤后,分别打印amountA和amountB的值作为玩家A和B双方的结果分数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum score
// obtained by the players A and B
void findAmountPlayers(int arr[], int N)
{
// Sort the array in descending order
sort(arr, arr + N, greater());
// Stores the maximum amount of
// money obtained by A
int amountA = 0;
// Stores the maximum amount of
// money obtained by B
int amountB = 0;
// If the value of N is 1
if (N == 1) {
// Update the amountA
amountA += arr[0];
// Print the amount of money
// obtained by both players
cout << "(A : " << amountA << "), "
<< "(B : " << amountB << ")";
return;
}
// Update the amountA
amountA = arr[0];
// Update the amountB
amountB = arr[1];
// Traverse the array arr[]
for (int i = 2; i < N; i++) {
// If i is an odd number
if (i % 2 == 0) {
// Update the amountB
amountB += arr[i];
}
else {
// Update the amountA
amountA += arr[i];
}
}
// Print the amount of money
// obtained by both the players
cout << "(A : " << amountA << ")\n"
<< "(B : " << amountB << ")";
}
// Driver Code
int main()
{
int arr[] = { 1, 1, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
findAmountPlayers(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the maximum score
// obtained by the players A and B
static void findAmountPlayers(int[] arr, int N)
{
// Sort the array in descending order
Arrays.sort(arr);
reverse(arr, N);
// Stores the maximum amount of
// money obtained by A
int amountA = 0;
// Stores the maximum amount of
// money obtained by B
int amountB = 0;
// If the value of N is 1
if (N == 1)
{
// Update the amountA
amountA += arr[0];
// Print the amount of money
// obtained by both players
System.out.println("(A : " + amountA + "), " +
"(B : " + amountB + ")");
return;
}
// Update the amountA
amountA = arr[0];
// Update the amountB
amountB = arr[1];
// Traverse the array arr[]
for(int i = 2; i < N; i++)
{
// If i is an odd number
if (i % 2 == 0)
{
// Update the amountB
amountB += arr[i];
}
else
{
// Update the amountA
amountA += arr[i];
}
}
// Print the amount of money
// obtained by both the players
System.out.println("(A : " + amountA + ")");
System.out.println("(B : " + amountB + ")");
}
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for(int i = 0; i < n; i++)
{
b[j - 1] = a[i];
j = j - 1;
}
}
// Driver code
public static void main(String []args)
{
int[] arr = { 1, 1, 1 };
int N = arr.length;
findAmountPlayers(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find the maximum score
# obtained by the players A and B
def findAmountPlayers(arr, N):
# Sort the array in descending order
arr = sorted(arr)[::-1]
# Stores the maximum amount of
# money obtained by A
amountA = 0
# Stores the maximum amount of
# money obtained by B
amountB = 0
# If the value of N is 1
if (N == 1):
# Update the amountA
amountA += arr[0]
# Prthe amount of money
# obtained by both players
print("(A :",mountA,"), (B :",str(amountB)+")")
return
# Update the amountA
amountA = arr[0]
# Update the amountB
amountB = arr[1]
# Traverse the array arr[]
for i in range(2, N):
# If i is an odd number
if (i % 2 == 0):
# Update the amountB
amountB += arr[i]
else:
# Update the amountA
amountA += arr[i]
# Print amount of money
# obtained by both the players
print("(A :",str(amountA)+")\n(B :",str(amountB)+")")
# Driver Code
if __name__ == '__main__':
arr = [1, 1, 1]
N = len(arr)
findAmountPlayers(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the maximum score
// obtained by the players A and B
static void findAmountPlayers(int[] arr, int N)
{
// Sort the array in descending order
Array.Sort(arr);
Array.Reverse(arr);
// Stores the maximum amount of
// money obtained by A
int amountA = 0;
// Stores the maximum amount of
// money obtained by B
int amountB = 0;
// If the value of N is 1
if (N == 1) {
// Update the amountA
amountA += arr[0];
// Print the amount of money
// obtained by both players
Console.WriteLine("(A : " + amountA + "), "
+ "(B : " + amountB + ")");
return;
}
// Update the amountA
amountA = arr[0];
// Update the amountB
amountB = arr[1];
// Traverse the array arr[]
for (int i = 2; i < N; i++) {
// If i is an odd number
if (i % 2 == 0) {
// Update the amountB
amountB += arr[i];
}
else {
// Update the amountA
amountA += arr[i];
}
}
// Print the amount of money
// obtained by both the players
Console.WriteLine("(A : " + amountA + ")");
Console.WriteLine("(B : " + amountB + ")");
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 1, 1 };
int N = arr.Length;
findAmountPlayers(arr, N);
}
}
// This code is contributed by ukasp.
Javascript
输出:
(A : 1)
(B : 2)
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。