Coins in a Line I
2024-10-21 14:38:00
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;
}
}
最新文章
- 在JavaScript和C#中获得referer
- Manacher&#39;s algorithm: 最长回文子串算法
- Message,MessageQueue,Looper,Handler详解+实例
- 小心C语言的定义与声明
- 如何解决自定义ToolBar起始位置的空格(左对齐)问题
- 使用EXCEL设置“下拉菜单”选项功能
- 2014牡丹江——Hierarchical Notation
- 大数据:Hadoop入门
- Git中一些远程库操作的细节
- Struts(十一):OGNL表达式(二)
- 消除点击连接或者按钮或者执行onclick事件时出现的边框
- p3966单词
- SQL Server进阶(一)T-SQL查询和编程的背景
- 振动器(Vibrator)
- Spring学习总结之高级装配
- scrapy 简单防封
- 【机器学习算法】bagging算法
- 20155339 2016-2017-2 《Java程序设计》第7周学习总结
- 2-8 字典dict
- »; Working Around JNI UTF-8 Strings Deprogramming
热门文章
- JavaWeb实现增删查改(图书信息管理)之删除功能实现
- C程序设计语言练习 第二章
- java笔记4—继承
- AVR单片机教程——数字输入
- Mitsubishi (三菱) Fanuc(发那科),CNC,网口数据采集,NC程序下发(其它品牌CNC,哈斯 马扎克 兄弟等,正在开发中)
- The Day Two 找到一个具有最大和的连续子数组,返回其最大和
- Jupyter交互式工具安装使用
- ubuntu配置Selenium+Chromedriver
- SQL Server 截取日期部分字符
- Java 常用API (第二部分)