Wednesday, July 27, 2022

[LeetCode] Predict the Winner

Problem: You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.

Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array.

Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.

Example:

Input: nums = [1,5,2]
Output: false
Explanation: Initially, player 1 can choose between 1 and 2. 
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). 
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. 
Hence, player 1 will never be the winner and you need to return false.
Input: nums = [1,5,233,7]
Output: true
Explanation: Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.


Approach: Few things which we can just guess by looking at the problem and different examples are if n <= 2, Player1 will always win, also if n is even then Player 1 will always win where n is the length of nums array.

Now let's see how we can solve it. Let's define a variable effective_score which will have the difference of current player score and other player score i.e.

effective_score = curr_player_score - other_player_score

where curr_player is the player whose turn is to choose the integer from nums.

Obviously if player 1's effective_score is >= 0 then player 1 wins, otherwise lose.

Now given num[i...j] array, current player effective_score would be

effective_score(nums[i...j)  = MAX(nums[i] - effective_score(nums[i + 1...j), nums[j] - effective_score(nums[i...j - 1])

Meaning of above recursive relation is as follows: current player chooses 

  • nums[i]:  effective_score will be nums[i] - effective_score of other player for nums[i + 1...j]  
  • nums[j]:  effective_score will be nums[j] - effective_score of other player for nums[i...j - 1]

Obviously we want to take the maximum of the above 2 to calculate the effective_score.

This will surely solve the problem but it will not be efficient. The size of the recursion tree will be 2^n here.

We can optimize it using DP. We can have a 2D table say Table, where Table[i][j] will tell the effective score of player 1 in the sub array nums[i...j]. We can simply fill this table as follows:

Table[i][j] = MAX(nums[i] - Table[i + 1][j], nums[j] - Table[i][j - 1])

Above line is just like the recursive relation so nothing to explain here.


Implementation in C#:

Approach 1: Recursion:

    public bool PredictTheWinner(int[] nums) 

    {

        if (nums.Length <= 2)

        {

            return true;

        }

        if (nums.Length % 2 == 0)

        {

            return true;

        }        

        return this.EffectiveScore(nums, 0, nums.Length - 1) >= 0;

    }

    

    private int EffectiveScore(int[] nums, int start, int end)

    {

        if (start == end)

        {

            return nums[start];

        }

        int scoreWithStart = nums[start] - this. EffectiveScore(nums, start + 1, end);

        int scoreWithEnd = nums[end] - this. EffectiveScore(nums, start, end - 1);

        return Math.Max(scoreWithStart, scoreWithEnd);

    }


Approach 2: DP:

    public bool PredictTheWinner(int[] nums) 

    {

        if (nums.Length <= 2)

        {

            return true;

        }

        if (nums.Length % 2 == 0)

        {

            return true;

        }

        int[,] table = new int[nums.Length, nums.Length];

        for (int i = 0; i < nums.Length; ++i)

        {

            table[i, i] = nums[i];

        }

        for (int i = nums.Length - 2; i >= 0; --i)

        {

            for (int j = i + 1; j < nums.Length; ++j)

            {

                table[i, j] = Math.Max(nums[i] - table[i + 1, j], nums[j] - table[i, j - 1]);

            }

        }

        return table[0, nums.Length - 1] >= 0;

    }


Complexity: Approach 1: O(2^n), Approach 2: O(n^2)

No comments:

Post a Comment