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?

Example

n = 1, return true.

n = 2, return true.

n = 3, return false.

n = 4, return true.

n = 5, return true.

分析:

既然可以拿1个和2个,那么只要coin的个数能够被3整除,那么第一个拿的百分百会输掉。

 1 public class Solution {
 2     /**
 3      * @param n: an integer
 4      * @return: a boolean which equals to true if the first player will win
 5      */
 6     public boolean firstWillWin(int n) {
 7         // write your code here
 8         if (n % 3 == 0) return false;
 9         return true;
10     }
11 }

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 win or lose?

Example

Given values array A = [1,2,2], return true.

Given A = [1,2,4], return false.

分析:

这里假设两个player都是rational player,也就是说两个人都会采取最优策略。

那么,这个最优策略怎么来的,对于当前player,他要么选取一个,或者两个,选取一个只能在下面两种情况中产生,假设当前index 为 i;

1. A[i] >= A[i + 1] + A[i + 2];

2. A[i + 1] + A[i + 2] - A[i] <= A[i + 2] + A[i + 3] - A[i] - A[i + 1]

选取两个这种策略只能在下面两种情况中产生:

1. A[i] + A[i + 1] >= A[i + 2] + A[i + 3];

2. A[i + 1] + A[i + 2] - A[i] > A[i + 2] + A[i + 3] - A[i] - A[i + 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.length <= 2) return true;
 8         int[] total = new int[2];
 9
10         boolean firstPlayerTurn = true;
11         int i = 0;
12         while (i < values.length) {
13                 if (i + 3 >= values.length) {
14                     if (i < values.length) {
15                         increase(values, i, total, firstPlayerTurn);
16                         i++;
17                     }
18                     if (i < values.length) {
19                         increase(values, i, total, firstPlayerTurn);
20                         i++;
21                     }
22                 } else {
23                     if (values[i] >= values[i + 1] + values[i + 2]) {
24                         increase(values, i, total, firstPlayerTurn);
25                         i++;
26                     } else if (values[i] + values[i + 1] >= values[i + 2] + values[i + 3]) {
27                         increase(values, i, total, firstPlayerTurn);
28                         i++;
29                         increase(values, i, total, firstPlayerTurn);
30                         i++;
31                     } else {
32                         int diff1 = values[i + 1] + values[i + 2] - values[i];
33                         int diff2 = values[i + 2] + values[i + 3] - values[i] - values[i + 1];
34                         if (diff1 < diff2) {
35                             increase(values, i, total, firstPlayerTurn);
36                             i++;
37                         } else {
38                             increase(values, i, total, firstPlayerTurn);
39                             i++;
40                             increase(values, i, total, firstPlayerTurn);
41                             i++;
42                         }
43                     }
44                 }
45                 if (firstPlayerTurn) {
46                     firstPlayerTurn = false;
47                 } else {
48                     firstPlayerTurn = true;
49                 }
50             }
51             return total[0] > total[1];
52         }
53
54     public void increase(int[] values, int index, int[] total, boolean firstPlayer) {
55         if (firstPlayer) {
56             total[0] += values[index];
57         } else {
58             total[1] += values[index];
59         }
60     }
61 }
时间: 2024-10-31 17:52:36

Coins in a Line I & II的相关文章

[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 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 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],试图给我们留更小的值,

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

Description 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