Description

There are n coins in a line, and value of i-th coin is values[i].

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

Example 1:

Input: [3, 2, 2]
Output: true
Explanation: The first player takes 3 at first. Then they both take 2.

Example 2:

Input: [1, 20, 4]
Output: false
Explanation: The second player will take 20 whether the first player take 1 or 4.

Challenge

O(1) memory and O(n) time when n is even.

思路:

区间动态规划问题, 具体定义状态的方式有很多种, 但是大同小异, 时空复杂度都相似. 这里只介绍 version 1 的具体实现.

设定 dp[i][j] 表示当前剩余硬币的区间为 [i, j] 时, 此时该拿硬币的人能获取的最大值.

注意, dp[i][j] 并没有包含角色信息, dp[0][values.length - 1] 表示的是先手的人能获得的最大值, 而 dp[1][values.length -1] 表示的则是后手的人能获得的最大值. 需要这样做是因为: 两个人都会采用最优策略.

当前的人的决策就是拿左边还是拿右边, 而下一个人仍然会最优决策, 所以应该是最小值中取最大值:

dp[i][j] = max(	                                     // 取max表示当前的人选用最优策略
min(dp[i + 2][j], dp[i + 1][j - 1]) + values[i], // 取min表示下一个人选用最优策略
min(dp[i][j - 2], dp[i + 1][j - 1]) + values[j]
)

几个边界:

i > j : dp[i][j] = 0
i == j : dp[i][j] = values[i]
i + 1 == j : dp[i][j] = max(values[i], values[j])
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[] values) { int n = values.length;
if (n == 0) {
return true;
} int[][] f = new int[n][n];
int i, j, len;
// len 1
for (i = 0; i < n; ++i) {
f[i][i] = values[i];
} for (len = 2; len <= n; ++len) {
for (i = 0; i <= n - len; ++i) {
j = i + len - 1;
f[i][j] = Math.max(values[i] - f[i + 1][j], values[j] - f[i][j - 1]);
}
} return f[0][n - 1] >= 0;
}
}

  

 

最新文章

  1. Unity性能优化(1)-官方教程The Profiler window翻译
  2. javascript-建造者模式
  3. BZOJ2960: 跨平面
  4. 在Windows 环境下编译Qt静态库(QT5.32)
  5. 软技能:十步学习法 (zhuan)
  6. DBA_Oracle Startup / Shutdown启动和关闭过程详解(概念)
  7. 在Eclipse中使用Github(EGit)
  8. url的4种访问方式
  9. 解决NGINX的WORDPRESS伪静态规则失效的问题
  10. es6笔记5^_^set、map、iterator
  11. python3.5 安装lxml
  12. tomcat配置单项HTTPS协议
  13. 如何编写51单片机超声波测距SR04_lcd1602显示程序
  14. CocoaPods 中删除不需要的第三方
  15. C++ 中用cin方式获取输入的几种常用方式
  16. spring mvc数据绑定与表单标签库
  17. Web Deploy发布网站错误 检查授权和委派设置
  18. C#反射详解
  19. [React] 03 - Intro: react.js in twelve demos
  20. python webdriver操作浏览器句柄

热门文章

  1. Kafka性能调优 - Kafka优化的方法
  2. Linux07 查找文件(find、locate)
  3. 函数的学习2——返回值&amp;传递列表——参考Python编程从入门到实践
  4. 51单片机局部变量占用ram的问题
  5. windows 开始→运行→命令集锦
  6. (父向子传值)组件内的properties类似与vue中的prop接收外界传递进来的参数
  7. AS3灰色图像
  8. AQS(AbstractQueuedSynchronizer)
  9. java之struts2之文件下载
  10. MySQL数据库连接报错