There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.
Could you please decide the first player will win or lose?
Example
Given values array A =[1,2,2], returntrue.
Given A =[1,2,4], returnfalse.
这次算的是是否得到一半以上的价值。具体解释看笔记,其实不太懂。
迭代dp:考虑的是当前取硬币的人最后最优能取多少
publicbooleanfirstWillWin(int[] values) {if (values ==null||values.length==0) {returnfalse; } elseif (values.length<3) {returntrue; }int n =values.length;int[] sum =newint[n +1];for (int i =1; i <= n; i++) { sum[i] = sum[i -1] + values[n - i]; }// because 1st player can choose take 1 or 2, so can take max (sum[i] - dp[i - 1], sum[i] - dp[i - 2])int[] dp =newint[n +1];// dp[i] means currently i coins left, max current player can get dp[1] = values[n -1];// 1 coin left, current player take it to get max dp[2] = values[n -1] + values[n -1];// 2 coins left, current player take both to get maxfor (int i =3; i <dp.length; i++) { dp[i] =Math.max(sum[i] - dp[i -1], sum[i] - dp[i -2]); }return dp[n] > sum[n] /2;}