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

If the first player wins, return true, otherwise return false.

Example

Example 1:

Input: [1, 2, 2]
Output: true
Explanation: The first player takes 2 coins.

Example 2:

Input: [1, 2, 4]
Output: false
Explanation: Whether the first player takes 1 coin or 2,
the second player will gain more value.
思路:博弈型动态规划。
public class Solution {
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int[] A) {
int n = A.length;
int[] f = new int[n + 1];
f[n] = 0;
int i;
for (i = n - 1; i >= 0; --i){
f[i] = A[i] - f[i + 1];
if (i < n - 1) {
f[i] = Math.max(f[i], A[i] + A[i + 1] - f[i + 2]);
}
} return f[0] >= 0;
}
}

  

 

最新文章

  1. 在JavaScript和C#中获得referer
  2. Manacher&#39;s algorithm: 最长回文子串算法
  3. Message,MessageQueue,Looper,Handler详解+实例
  4. 小心C语言的定义与声明
  5. 如何解决自定义ToolBar起始位置的空格(左对齐)问题
  6. 使用EXCEL设置“下拉菜单”选项功能
  7. 2014牡丹江——Hierarchical Notation
  8. 大数据:Hadoop入门
  9. Git中一些远程库操作的细节
  10. Struts(十一):OGNL表达式(二)
  11. 消除点击连接或者按钮或者执行onclick事件时出现的边框
  12. p3966单词
  13. SQL Server进阶(一)T-SQL查询和编程的背景
  14. 振动器(Vibrator)
  15. Spring学习总结之高级装配
  16. scrapy 简单防封
  17. 【机器学习算法】bagging算法
  18. 20155339 2016-2017-2 《Java程序设计》第7周学习总结
  19. 2-8 字典dict
  20. &raquo; Working Around JNI UTF-8 Strings Deprogramming

热门文章

  1. JavaWeb实现增删查改(图书信息管理)之删除功能实现
  2. C程序设计语言练习 第二章
  3. java笔记4—继承
  4. AVR单片机教程——数字输入
  5. Mitsubishi (三菱) Fanuc(发那科),CNC,网口数据采集,NC程序下发(其它品牌CNC,哈斯 马扎克 兄弟等,正在开发中)
  6. The Day Two 找到一个具有最大和的连续子数组,返回其最大和
  7. Jupyter交互式工具安装使用
  8. ubuntu配置Selenium+Chromedriver
  9. SQL Server 截取日期部分字符
  10. Java 常用API (第二部分)