当前位置:网站首页>LeetCode877. 石子游戏
LeetCode877. 石子游戏
2022-06-28 21:00:00 【Yuyy】
本文最后更新于 482 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
第一时间想到的是动态规划,后来看题解发现,这玩意没那么复杂。 A和B比赛,没有平局,有两种结局。一、A赢B输,二、A输B赢。但是题中说亚历克斯先开始,言外之意就是主动权在他那里,只需提前计算出A赢还是B赢,然后照着赢的那个做即可。 言而总之,只要亚历克斯想赢,就一定能赢。而题中说了假设亚历克斯和李都发挥出最佳水平,亚历克斯一定会发挥好的,所以他一定能赢。
二、问题
亚历克斯和李用几堆石子在做游戏。偶数堆石子排成一行,每堆都有正整数颗石子 piles[i] 。
游戏以谁手中的石子最多来决出胜负。石子的总数是奇数,所以没有平局。
亚历克斯和李轮流进行,亚历克斯先开始。 每回合,玩家从行的开始或结束处取走整堆石头。 这种情况一直持续到没有更多的石子堆为止,此时手中石子最多的玩家获胜。
假设亚历克斯和李都发挥出最佳水平,当亚历克斯赢得比赛时返回 true ,当李赢得比赛时返回 false 。
示例:
输入:[5,3,4,5]
输出:true
解释:
亚历克斯先开始,只能拿前 5 颗或后 5 颗石子 。
假设他取了前 5 颗,这一行就变成了 [3,4,5] 。
如果李拿走前 3 颗,那么剩下的是 [4,5],亚历克斯拿走后 5 颗赢得 10 分。
如果李拿走后 5 颗,那么剩下的是 [3,4],亚历克斯拿走后 4 颗赢得 9 分。
这表明,取前 5 颗石子对亚历克斯来说是一个胜利的举动,所以我们返回 true 。提示:
2 <= piles.length <= 500piles.length是偶数。1 <= piles[i] <= 500sum(piles)是奇数。
Related Topics
- 极小化极大
- 数学
- 动态规划
\n
- 220
- 0
三、代码
public boolean stoneGame(int[] piles) {
return true;
}Post Views: 347
边栏推荐
- [try to hack] cobalt strike (I)
- Employee salary management system
- 视频号如何下载视频?来看超简单方法!
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- Résumé de la stabilité
- Ehcache配置资料,方便自己查
- LeetCode每日一题——324. 摆动排序 II
- Leetcode daily question - 515 Find the maximum value in each tree row
- Pie (poj3122) super detailed and easy to understand binary introduction
- 炒股票能赚钱么?开户安全嘛
猜你喜欢

On the complexity of software development and the way to improve its efficiency

【学习笔记】主成分分析法介绍

API 网关 Apache APISIX 助力雪球双活架构演进

The principle and source code analysis of Lucene index construction
oracle delete误删除表数据后如何恢复

不同框架的绘制神经网络结构可视化

【Try to Hack】Cobalt Strike(一)

Leetcode 36. Effective Sudoku (yes, once)

CNN-LSTM的flatten

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
随机推荐
Leetcode daily question - 515 Find the maximum value in each tree row
Globalsign's Pan domain SSL certificate
LeetCode560. 和为K的子数组
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
MySQL system error occurred 1067
How to use dataant to monitor Apache apisex
Anr no response introduction
Ehcache配置资料,方便自己查
Leetcode daily question - 324 Swing sort II
How to analyze the relationship between enterprise digital transformation and data asset management?
Leetcode 36. 有效的数独(可以,一次过)
How to analyze the relationship between enterprise digital transformation and data asset management?
On the complexity of software development and the way to improve its efficiency
Input and output real data
Can you make money by speculating in stocks? It's safe to open an account
LeetCode每日一题——30. 串联所有单词的子串
The further application of Li Kou tree
Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
ThreadLocal principle
精通数据分析能力,收入翻倍?什么才是最强竞争力