Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
f70b6318
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f70b6318
编写于
3月 28, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新 stone game
上级
91557e42
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
41 addition
and
33 deletion
+41
-33
877.stone-game.md
877.stone-game.md
+41
-33
未找到文件。
877.stone-game.md
浏览文件 @
f70b6318
## 题目地址
https://leetcode.com/problems/stone-game/description/
## 题目描述
...
...
@@ -13,19 +13,19 @@ Alex and Lee take turns, with Alex starting first. Each turn, a player takes th
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: [5,3,4,5]
Output: true
Explanation:
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Note:
...
...
@@ -35,33 +35,34 @@ piles.length is even.
sum(piles) is odd.
```
## 思路
由于
piles是偶数的,并且piles
的总和是奇数的。
由于
piles 是偶数的,并且 piles
的总和是奇数的。
因此
Alex
`可以做到`
要不拿的全部是奇数,要么全部是偶数。
因此
Alex
`可以做到`
要不拿的全部是奇数,要么全部是偶数。
举个例子: 比如
Alex
第一次先拿第一个
举个例子: 比如
Alex
第一次先拿第一个
这里有两种情况:
1.
Lee如果拿了第二块(偶数),那么Alex继续拿第三块,以此类推。。。
2.
Lee如果拿了最后一块(偶数),那么Alex继续拿倒数第二块,以此类推。。。
1.
Lee 如果拿了第二块(偶数),那么 Alex 继续拿第三块,以此类推。。。
2.
Lee 如果拿了最后一块(偶数),那么 Alex 继续拿倒数第二块,以此类推。。。
因此Alex
`可以`
做到只拿奇数或者偶数,只是他可以控制的,因此他要做的就是数一下,奇数加起来多还是偶数加起来多就好了。
奇数多就全部选奇数,偶数就全部选偶数。 Lee是没有这种自由权的。
因此
Alex
`可以`
做到只拿奇数或者偶数,只是他可以控制的,因此他要做的就是数一下,奇数加起来多还是偶数加起来多就好了。
奇数多就全部选奇数,偶数就全部选偶数。 Lee
是没有这种自由权的。
## 关键点解析
-
可以用DP(动态规划)
-
可以用
DP(动态规划)
-
可以从数学的角度去分析
> ......(😅)
## 代码
```
js
/*
* @lc app=leetcode id=877 lang=javascript
...
...
@@ -79,26 +80,26 @@ sum(piles) is odd.
* 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 is odd, so there are no ties.
*
*
* Alex and Lee take turns, with Alex starting first. Each turn, a player
* takes the entire pile of stones from either the beginning or the end of the
* row. This continues until there are no more piles left, at which point the
* person with the most stones wins.
*
*
* Assuming Alex and Lee play optimally, return True if and only if Alex wins
* the game.
*
*
*
*
*
*
* Example 1:
*
*
*
*
* Input: [5,3,4,5]
* Output: true
* Explanation:
* Explanation:
* Alex starts first, and can only take the first 5 or the last 5.
* Say he takes the first 5, so that the row becomes [3, 4, 5].
* If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10
...
...
@@ -107,28 +108,35 @@ sum(piles) is odd.
* with 9 points.
* This demonstrated that taking the first 5 was a winning move for Alex, so we
* return true.
*
*
*
*
*
*
*
*
* Note:
*
*
*
*
* 2 <= piles.length <= 500
* piles.length is even.
* 1 <= piles[i] <= 500
* sum(piles) is odd.
*
*
*
*
*
*
*/
/**
* @param {number[]} piles
* @return {boolean}
*/
var
stoneGame
=
function
(
piles
)
{
return
true
;
return
true
;
};
```
## 扩展
腾讯面试题:一共 100 只弓箭 你和你的对手共用。你们每次只能射出一支箭或者两支箭,射击交替进行,设计一个算法,保证自己获胜。
答案: 先手,剩下的是 3 的倍数就行(100-1=99),然后按照 3 的倍数射箭必赢。
比如你先拿了 1,剩下 99 个。 对手拿了 1,你就拿 2。这样持续 33 次就赢了。如果对手拿了 2 个,你就拿 1 个,这样持续 33 次你也是赢的。
> 这是一种典型的博弈问题, 你和对手交替进行,对手的行动影响你接下来的策略。 这算是一种最简单的博弈问题了
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录