Coins in a Line I Solution

第一个游戏者永远拿不到第3n枚硬币,所以在硬币总数不能被3整除的情况下,都可以赢。

public class Solution {public boolean firstWillWin(int n) {return n % 3 != 0; }
}

Coins in a Line II

Problem

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.

Note

DP做法,设dp[i]为第一个游戏者从第i枚硬币到end能获得硬币价值的最大值。

Solution

Fool Lintcode method: it happened to work! But it's completely wrong!

public class Solution {public boolean firstWillWin(int[] A) {// write your code hereif (A == null) return false;int sum = 0;for (int i = 1; i <= A.length / 3; i++) {sum += A[3*i-1];}int total = 0;for (int i = 0; i < A.length; i++) {total += A[i];}if (sum * 2 > total) return false;return true;}
}

DP method

主要参考这篇文章的解释

http://www.mamicode.com/info-...

public class Solution {public boolean firstWillWin(int[] values) {// write your code hereint len = values.length;if (len <= 2) {return true;}//dp[i] means the largest value you(the first player) //can get when you start from values[i] int[] dp = new int[len+1];//not even existdp[len] = 0;//when you happen to have the last coin, yes, consider the last firstdp[len-1] = values[len-1];//sure we should get the last two for most valuedp[len-2] = values[len-1] + values[len-2];//same rules, why leave two(len-1, len-2) for the other playerdp[len-3] = values[len-2] + values[len-3];//next we are gonna sum upfor (int i = len-4; i >= 0; i--) {//you have to have values[i] and the non-optimal later choice//because the other player is smart to leave you the worse one//between two of your optimal choicesdp[i] = values[i] + Math.min(dp[i+2], dp[i+3]);dp[i] = Math.max(dp[i], values[i] + values[i+1] + Math.min(dp[i+3], dp[i+4]));//equals to: dp[i] = Math.max(values[i] + Math.min(dp[i+2],dp[i+3]), values[i] + values[i+1] + Math.min(dp[i+3], dp[i+4]));}//compute the total value of coinsint sum = 0;for (int a: values) {sum += a;   }//compare your final value to the other player'sreturn dp[0] > sum - dp[0];}
}

Now let's make the code elegant


public class Solution {public boolean firstWillWin(int[] values) {if (values == null || values.length == 0) return false;int n = values.length;if (n < 3) return true;int[] dp = new int[n+1];dp[n] = 0;dp[n-1] = values[n-1];dp[n-2] = values[n-1]+values[n-2];dp[n-3] = values[n-2]+values[n-3];for (int i = n-4; i >= 0; i--) {dp[i] = Math.max(values[i] + Math.min(dp[i+2], dp[i+3]), values[i] + values[i+1] + Math.min(dp[i+3], dp[i+4]));}int sum = 0;for (int v: values) sum += v;return dp[0] > sum - dp[0];}
}

[LintCode] Coins in a Line I Coins in a Line II相关推荐

  1. android.view.InflateException: Binary XML file line #7: Binary XML file line #7

    错误如下 11-21 08:19:44.040 3608-3608/com.leon.oldrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main   ...

  2. android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解

    话不多少,上错误堆栈: Process: com.mci.smagazine, PID: 25065java.lang.RuntimeException: Unable to start activi ...

  3. vty 虚拟终端连接 line vty 0 4 和line vty 5 15 区别

    在思科交换机Catalyst 2950上show run下发现有两个line vty line vty 0 4 和line vty 5 15 分别有相应密码 .. 请问这两个有什么区别? VTY是Ci ...

  4. android line分享代码,Android实现Line登录分享

    一.获取参数 1.注册登录Line开发者账号 在Line官网并找不到注册地方,可以通过Line APP进行注册,注册之后进入Line开发者官网: 2.创建应用 3.创建完成在Channel setti ...

  5. python line strip_关于python 的line.strip()方法

    测试文本 abc abcd show me the money 代码一: def showfile (filepath): startTime=datetime.datetime.now() f=op ...

  6. android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating

    android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating cla ...

  7. From line 6, column 36 to line 6, column 71: Cannot apply ‘-‘ to arguments of type ‘<VARCHAR(214748

    报错信息: From line 6, column 36 to line 6, column 71: Cannot apply '-' to arguments of type '<VARCHA ...

  8. line vty 0 4 和line vty 5 15 有什么区别

    在思科交换机Catalyst 2950上show run下发现有两个line vty line vty 0 4 和line vty 5 15 分别有相应密码 .. 请问这两个有什么区别? VTY是Ci ...

  9. Vue 关于ESLint语法规范报错:Line 10 exceeds the maximum line length of 100

    ESLint语法报错 最近在使用vue-cli4创建项目,在项目编译时,报出来了一些语法错误; 因为我使用的是bootstrap组件,所以在一行引用时报出来: Line 10 exceeds the ...

  10. 解决报错 Error in processing command line: Don‘t understand command line argument “-cl-no-subgroup-ifp“

    调用openpose进行姿势检测,发现报错(虽然报错但能继续运行,输出检测结果) OpenCV(ocl4dnn): consider to specify kernel configuration c ...

最新文章

  1. sys.check_constraints
  2. large graph挖掘的技术基础
  3. OpenStack 业务链networking-sfc介绍 (2) - 底层原理
  4. 【C语言】C语言实现面向对象编程之继承
  5. python中seed的用法_Python中的seed()方法怎么用
  6. (37)FPGA面试技能提升篇(IUS仿真工具)
  7. SQL Server Junior Database Administrator方案相关的访谈问答
  8. 面部识别 vs 情绪状态,你还能守住自己的秘密吗?
  9. 一名技术leader的工作随笔
  10. java 反编译 exe_Java反编译
  11. mysql安装包及安装教程(附网盘地址)
  12. 安卓模拟器genymotion安装设置修改IMEI
  13. 按键精灵手机助手学习笔记
  14. 关于Google Chrome浏览器离线安装包下载方法
  15. Meta-Tracker(ECCV 2018)视频目标跟踪源码运行笔记——Testing模式
  16. 亚马逊商品详情API接口(item_get-获得AMAZON商品详情接口),亚马逊API接口
  17. MySQL中怎么对varchar类型排序问题(数字字符串和汉字拼音的顺序)
  18. OSChina 周六乱弹 —— 成功的解决了发现问题的人
  19. EH集团筹集逾500万瑞士法郎,用于推进其零排放燃料电池技术
  20. Container Station搭建个人网盘Nextcloud(Mariadb)

热门文章

  1. 谷歌小姐姐搞出魔法画板:你随便画,补不齐算AI输
  2. 自动驾驶帆船,有史以来第一次成功横渡大西洋
  3. 美国下注15亿美元重点搞芯片!电子复兴5年计划首批入围项目曝光
  4. 何恺明!再斩ICCV 2017最佳论文
  5. 简单python爬虫案例(爬取慕课网全部实战课程信息)
  6. Android事件总线(四)源码解析otto
  7. PHP代码重用与函数编写
  8. 雪球:如果让你选择一本影响你一生的好书,你会选择哪一本
  9. POJ-1001 求高精度幂
  10. 树莓派RaspberryPi的RPi.GPIO使用指南