Coins in a Line II

ref: http://www.lintcode.com/en/problem/coins-in-a-line-ii/#



有个博客说的挺好的,比九章自己写的清楚

http://www.cnblogs.com/theskulls/p/4963317.html

  • 意思是这样的:

如果我们在第i个位置,那么我们有两种选择,我们要选取里面更大的

1. 我们只拿一个values[i],那么对方也是很机智的,他会试图给我们最差的结果,会选择拿一个[i+1]或者两个[i+1]&[i+2],试图给我们留更小的值,所以我们自动拿到了dp[i+2](对方拿一个)和dp[i+3](对方拿了两个)中比较小的那个,此时dp[i]_1 = values[i] + min{dp[i+2], dp[i+3]};

2. 我们拿了两个数, values[i]&values[i+1],和上一种可能里一样,机智的对手会给我们剩下dp[i+3]和dp[i+4]里比较小的那个, 此时dp[i]_1 = values[i]+values[i+1]+min{dp[i+3], dp[i+4]};

所以结果是dp[i] = max{dp[i]_1, dp[i]_2}

  • 我们可以想象是从右往左推导,因为前面的结果需要确定的后面的结果
  • 初始化的问题:首先我们可以人工分析出,如果有两个及以下的结果,就是都选了,如果有三个数的时候,就从左往右选前两个,因为就算不能赢也尽量给对手少点。但是到四个的时候,这个就不一定了,要根据具体的数字分析,我们不能强行初始化了。但是dp的时候又需要用到dp[i+4],所以只能多加一格,初始化成0,来辅助dp

感觉理解还是不够深入,以后多做几遍可能感觉会好一点

 1     public boolean firstWillWin(int[] values) {
 2         int len = values.length;
 3         if(len == 0) {
 4             return false;
 5         }
 6         if(len == 1 || len == 2) {
 7             return true;
 8         }
 9         int[] dp = new int[len + 1];
10         int allSum = 0;
11         dp[len] = 0;
12         dp[len - 1] = values[len - 1];
13         dp[len - 2] = values[len - 2] + values[len - 1];
14         dp[len - 3] = values[len - 3] + values[len - 2];
15         allSum = dp[len - 3] + values[len - 1];
16         for(int i = len - 4; i >= 0; i--) {
17             allSum += values[i];
18             dp[i] = values[i] + Math.min(dp[i + 2], dp[i + 3]);
19             dp[i] = Math.max(dp[i], values[i] + values[i + 1] + Math.min(dp[i + 3], dp[i + 4]));
20         }
21         return dp[0] > allSum - dp[0];
22     }
时间: 2024-08-15 04:23:37

Coins in a Line II的相关文章

[LintCode] Coins in a Line II

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

Coins in a Line I & II

Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins. Could you please decide the first play will win or lose? Exampl

[LintCode] Coins in a Line III

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

LeetCode Coins in a Line

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins. Could you please decide the first play will win or lose? 该题类似于下题: 箱子里面有一百个球,甲和乙分别

Coins in a Line

ref: http://www.lintcode.com/en/problem/coins-in-a-line/ There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins. Could you please decide

[LintCode] Coins in a Line 一条线上的硬币

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins. Could you please decide the first play will win or lose? Have you met this questi

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? Given

[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.  The total number of stones

lintcode 中等题:Coins in Line II 硬币排成线 II

题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是输还是赢? 样例 给定数组 A = [1,2,2], 返回 true. 给定数组 A = [1,2,4], 返回 false. 解题 动态规划.博弈论 坑死,看了好久 定义dp[i]表示从i到end能取到的最大值 当我们在i处,有两种选择: 1.若取values[i],对方可以取values[i+1