Coins in a Line III
There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.
Could you please decide the first player will win or lose?
Example
Given array A = [3,2,2], return true.
Given array A = [1,2,4], return true.
Given array A = [1,20,4], return false.
思路:1. 与Coins II 不同的是玩家可以从两端中的一端取一枚硬币。既然只能从两端取硬币,那么就会存在一段连续的硬币区间。要想表示这个区间需要采用二维数组。
2. DP[i][j] 表示在i 到 j 这段区间里,先手能获取到的最大值。 sum[i][j] 表示 这段区间硬币值的总和, 那么DP[i][j] = sum[i][j] - MIN{DP[i + 1][j], DP[i][j - 1]}。
1 public class Solution { 2 /** 3 * @param values: an array of integers 4 * @return: a boolean which equals to true if the first player will win 5 */ 6 public boolean firstWillWin(int[] values) { 7 if (values == null || values.length == 0) { 8 return true; 9 } 10 int n = values.length; 11 int[][] dp = new int[n + 1][n + 1]; 12 int[][] sum = new int[n + 1][n + 1]; 13 for (int i = 1; i <= n; i++) { 14 for (int j = i; j <= n; j++) { 15 sum[i][j] = sum[i][j - 1] + values[j - 1]; 16 } 17 } 18 for (int i = 1; i <= n; i++) { 19 dp[i][i] = values[i - 1]; 20 } 21 for (int i = n - 1; i >= 1; i--) { 22 for (int j = i + 1; j <= n; j++) { 23 int min = Math.min(dp[i + 1][j], dp[i][j - 1]); 24 dp[i][j] = sum[i][j] - min; 25 } 26 } 27 return dp[1][n] > sum[1][n] / 2; 28 } 29 }

浙公网安备 33010602011771号