Predict the Winner LT486

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.

Example 1:

Input: [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.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have 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.

Note:

  1. 1 <= length of the array <= 20.
  2. Any scores in the given array are non-negative integers and will not exceed 10,000,000.
  3. If the scores of both players are equal, then player 1 is still the winner. 

Idea 1.a backtracking with memory. For the first player to win, the score score1 >=  score2 for the second player, while score1 + score2 = sum of all scores in the array, it mean score1 * 2 >= sum. Let dp(a, b) represent the maxmum score of player1 when choosing in the range nums[a, b], based on the observation and -max(-a, -b) = min(a, b), we can get the followig equation:

dp(a, b) = max(sum(a, b) - dp(a+1, b), sum(a, b) - dp(a, b-1))

           = max(nums[a] - max(-dp(a+2, b), -dp(a+1, b-1),

                    nums[b] - max(-dp(a+1, b-1), -dp(a, b-2)))      

           = max(nums[a] + min(dp(a+2, b), dp(a+1, b-1)),

                    nums[b] + min(dp(a+1, b-1), dp(a, b-2)))

Time complexity: O(n2)

Space complexity: O(n2)

 1 class Solution {
 2    private int maxScoreForRange(int[] nums, int a, int b, int[][] maxScores) {
 3        if(a > b) {
 4            return 0;
 5        }
 6        
 7        if(maxScores[a][b] == 0) {
 8            int scoreA = nums[a] + Math.min(maxScoreForRange(nums, a+2, b, maxScores), maxScoreForRange(nums, a+1, b-1, maxScores));
 9            int scoreB = nums[b] + Math.min(maxScoreForRange(nums, a+1, b-1, maxScores), maxScoreForRange(nums, a, b-2, maxScores));
10            maxScores[a][b] = Math.max(scoreA, scoreB);
11        }
12        
13        return maxScores[a][b];
14    }
15     
16     public boolean PredictTheWinner(int[] nums) {
17         int sum = 0;
18         for(int num: nums) {
19             sum += num;
20         }
21         
22         int[][] maxScores = new int[nums.length][nums.length];
23         if(maxScoreForRange(nums, 0, nums.length-1, maxScores) * 2 >= sum) {
24             return true;
25         }
26         return false;
27     }
28 }

Idea 1.b, recursion, based on the idea that score1 >= score2, score1 - score2 >= 0, avoid the computation to calculate the sum. We use turn to represent player1 (turn =1 )  and player2(turn = -1),  and switch turn based on the player.

Note: the returned value is given by turn * max(turn*b, turn *b), equivalently max(a, b) for player1's turn and min(a, b) for player2's turn.

Time complexity: O(2^n), size of recursion tree is 2^n

Space complexity: O(n) the depth of the recursion tree

 1 class Solution {
 2    private int differenceForRange(int[] nums, int a, int b, int turn) {
 3        if(a > b) {
 4            return 0;
 5        }
 6        
 7        int scoreA = turn * nums[a] + differenceForRange(nums, a+1, b, -turn);
 8        int scoreB = turn * nums[b] + differenceForRange(nums, a, b-1, -turn);
 9        
10        return turn * Math.max(turn * scoreA, turn * scoreB);
11    }
12     
13     public boolean PredictTheWinner(int[] nums) {
14         
15         if(differenceForRange(nums, 0, nums.length-1, 1) >= 0) {
16             return true;
17         }
18         return false;
19     }
20 }

with variable turn, add the choice when it's player1's turn, subtract the choice when it's player2's turn.

Time complexity: O(2^n)

Space complexity: O(n)

 1 class Solution {
 2    private int differenceForRange(int[] nums, int a, int b) {
 3        if(a > b) {
 4            return 0;
 5        }
 6        
 7        int scoreA = nums[a] - differenceForRange(nums, a+1, b);
 8        int scoreB = nums[b] - differenceForRange(nums, a, b-1);
 9        
10        return Math.max(scoreA, scoreB);
11    }
12     
13     public boolean PredictTheWinner(int[] nums) { 
14         return differenceForRange(nums, 0, nums.length-1) >= 0;
15     }
16 }

Idea 1.c recusion + memory

Time complexity: O(n2)

Space complexity: O(n2)

 1 class Solution {
 2    private int differenceForRange(int[] nums, int a, int b, Integer[][] diffScores) {
 3        if(a > b) {
 4            return 0;
 5        }
 6        
 7        if(diffScores[a][b] == null) {
 8          int scoreA = nums[a] - differenceForRange(nums, a+1, b, diffScores);
 9          int scoreB = nums[b] - differenceForRange(nums, a, b-1, diffScores);
10        
11          diffScores[a][b] =  Math.max(scoreA, scoreB);
12        }
13        
14        return diffScores[a][b];
15    }
16     
17     public boolean PredictTheWinner(int[] nums, Integer[][] diffScores) { 
18         Integer[][] diffScores = new Integer[nums.length][nums.length];
19         return differenceForRange(nums, 0, nums.length-1, diffScores) >= 0;
20     }
21 }

Idea 2. Dynamic programming based on the equation about diffence on score:

dp(a, b) = Math.max(nums[a]  - dp(a+1, b), nums[b] - dp(a, b-1))

 1 class Solution { 
 2     public boolean PredictTheWinner(int[] nums) { 
 3         int[][] scores = new int[nums.length][nums.length];
 4         
 5         for(int i = nums.length-1; i >= 0; --i) {
 6                 scores[i][i] = nums[i];
 7             for(int len = 2; i + len - 1 < nums.length; ++len) {
 8                 int j = i + len - 1;
 9                 scores[i][j] = Math.max(nums[i] - scores[i+1][j], 
10                                         nums[j] - scores[i][j-1]);
11             }
12         }
13         return scores[0][nums.length-1] >= 0;
14     }
15 }

Time complexity: O(n2)

Space complexity: O(n2)

Idea 3. Based on the above equation, we can use row instead of matrix by updating from left to right as previous value on the same row is needed and down to up as traversing the hidden matrix via a row.

row(b) = max(nums[a] - row(b_old), nums[b] - row[b-1])

Time complexity: O(n2)

Space complexity: O(n)

 1 class Solution { 
 2     public boolean PredictTheWinner(int[] nums) { 
 3         int[] row = new int[nums.length];
 4         
 5         for(int i = nums.length-1; i >= 0; --i) {
 6                 row[i] = nums[i];
 7             for(int len = 2; i + len - 1 < nums.length; ++len) {
 8                 int j = i + len - 1;
 9                 row[j] = Math.max(nums[i] - row[j], 
10                                   nums[j] - row[j-1]);
11             }
12         }
13         return row[nums.length-1] >= 0;
14     }
15 }

python:

 1 class Solution:
 2     def DiffScoresForRange(self, nums: List[int], a: int, b: int, diffScores) -> int:
 3         if a > b:
 4             return 0
 5         
 6         if diffScores[a][b] == None :
 7             scoreA = nums[a] - self.DiffScoresForRange(nums, a+1, b, diffScores)
 8             scoreB = nums[b] - self.DiffScoresForRange(nums, a, b-1, diffScores)
 9             diffScores[a][b] = max(scoreA, scoreB)
10         
11         return diffScores[a][b]
12         
13     def PredictTheWinner(self, nums: List[int]) -> bool:
14         diffScores = [[None for j in range(len(nums))] for i in range(len(nums))]
15         return self.DiffScoresForRange(nums, 0, len(nums)-1, diffScores) >= 0
 1 class Solution:      
 2     def PredictTheWinner(self, nums: List[int]) -> bool:
 3         diffScores = [[0 for j in range(len(nums))] for i in range(len(nums))]
 4         
 5         for i in range(len(nums)-1, -1, -1):
 6             diffScores[i][i] = nums[i]
 7             for length in range(2, len(nums)+1-i, 1):
 8                 j = i + length - 1
 9                 diffScores[i][j] = max(nums[i] - diffScores[i+1][j],
10                                       nums[j] - diffScores[i][j-1])
11 
12         return diffScores[0][len(nums)-1] >= 0
 1 class Solution:      
 2     def PredictTheWinner(self, nums: List[int]) -> bool:
 3         row = [0 for i in range(len(nums))]
 4         
 5         for i in range(len(nums)-1, -1, -1):
 6             row[i] = nums[i]
 7             for length in range(2, len(nums)+1-i, 1):
 8                 j = i + length - 1
 9                 row[j] = max(nums[i] - row[j],
10                              nums[j] - row[j-1])
11                 
12         return row[len(nums)-1] >= 0

posted on 2019-03-07 06:35  一直走在路上  阅读(156)  评论(0)    收藏  举报

导航