首页 > 其他 > 详细

LintCode Coins in a Line III

时间:2017-02-17 17:07:20      阅读:139      评论:0      收藏:0      [点我收藏+]

原题链接在这里:http://www.lintcode.com/en/problem/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.

题解:

类似Coins in a Line II.

DP问题. 状态dp[l][r]第i到第j的硬币时,先手去硬币的人最后最多去硬币价值.

转移方程, pickLeft = min(dp[l+2][r], dp[l+1][r-1])+values[l]

     pickRight = min(dp[l][r-2], dp[l-1][r-1])+values[r]

     dp[l][r] = max(pickLeft, pickRight)

初始化l == r时, dp[l][r] = values[l]

        l+1 == r时, dp[l][r] = max(values[l], values[l+1])

答案dp[0][values.length-1] > sum/2

Time Complexity: O(n). Space: O(n^2).

AC Java:

 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 false;
 9         }
10         
11         int n = values.length;
12         int [][] dp = new int[n][n];
13         boolean [][] used = new boolean[n][n];
14         int sum = 0;
15         for(int val : values){
16             sum += val;
17         }
18         return sum < 2*memorySearch(values, 0, values.length-1, dp, used);
19     }
20     
21     private int memorySearch(int [] values, int l, int r, int [][] dp, boolean [][] used){
22         if(used[l][r]){
23             return dp[l][r];
24         }
25         used[l][r] = true;
26         if(l>r){
27             dp[l][r] = 0;
28         }else if(l == r){
29             dp[l][r] = values[l];
30         }else if(l + 1 == r){
31             dp[l][r] = Math.max(values[l], values[l+1]);
32         }else{
33             int pickLeft = Math.min(memorySearch(values, l+2, r, dp, used), memorySearch(values, l+1, r-1, dp, used)) + values[l];
34             int pickRight = Math.min(memorySearch(values, l+1, r-1, dp, used), memorySearch(values, l, r-2, dp, used)) + values[r];
35             dp[l][r] = Math.max(pickLeft, pickRight);
36         }
37         return dp[l][r];
38     }
39 }

 

LintCode Coins in a Line III

原文:http://www.cnblogs.com/Dylan-Java-NYC/p/6410366.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!