📜  在消除硬币的游戏中,玩家双方可以收集的最大金钱

📅  最后修改于: 2021-09-04 09:40:45             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,其中arr[i]代表硬币的价值,任务是找到当两个玩家AB玩游戏时每个玩家可以获得的最大金额按照以下规则进行最佳游戏:

  • 玩家A总是开始游戏。
  • 在每一轮中,玩家必须从给定的数组中取出恰好 1 个硬币
  • 在每一轮中,玩家B必须从给定的数组中移除恰好 2 个硬币

例子:

方法:可以使用贪心方法解决给定的问题。请按照以下步骤解决问题:

  • 将两个变量(例如amountAamountB )初始化为0 ,用于存储玩家AB获得的总金额。
  • 按降序对给定数组进行排序。
  • 如果N 的值为1 ,则将amountA的值更新为arr[0]
  • 如果N的值大于或等于2 ,则将amountA的值更新为arr[0] ,将amountB的值更新arr[1]
  • 使用变量i在范围[2, N] 上遍历数组arr[] ,如果 i 的值是偶数,则将值arr[i] 添加amountB 。否则,将值arr[i]添加到amountA
  • 完成上述步骤后,分别打印amountAamountB的值作为玩家AB双方的结果分数。

下面是上述方法的实现:

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]
 
    # Prthe 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)

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