当前位置:网站首页>【LeetCode】877-石子游戏
【LeetCode】877-石子游戏
2022-07-02 12:09:00 【酥酥~】
Alice 和 Bob 用几堆石子在做游戏。一共有偶数堆石子,排成一行;每堆都有 正 整数颗石子,数目为 piles[i] 。
游戏以谁手中的石子最多来决出胜负。石子的 总数 是 奇数 ,所以没有平局。
Alice 和 Bob 轮流进行,Alice 先开始 。 每回合,玩家从行的 开始 或 结束 处取走整堆石头。 这种情况一直持续到没有更多的石子堆为止,此时手中 石子最多 的玩家 获胜 。
假设 Alice 和 Bob 都发挥出最佳水平,当 Alice 赢得比赛时返回 true ,当 Bob 赢得比赛时返回 false 。
示例 1:
输入: piles = [5,3,4,5]
输出: true
解释:
Alice 先开始,只能拿前 5 颗或后 5 颗石子 。
假设他取了前 5 颗,这一行就变成了 [3,4,5] 。
如果 Bob 拿走前 3 颗,那么剩下的是 [4,5],Alice 拿走后 5 颗赢得 10 分。
如果 Bob 拿走后 5 颗,那么剩下的是 [3,4],Alice 拿走后 4 颗赢得 9 分。
这表明,取前 5 颗石子对 Alice 来说是一个胜利的举动,所以返回 true 。
示例 2:
输入: piles = [3,7,2,3]
输出: true
提示:
- 2 <= piles.length <= 500
- piles.length 是 偶数
- 1 <= piles[i] <= 500
- sum(piles[i]) 是 奇数
思路
参照预测赢家一题
#动态规划
class Solution(object):
def stoneGame(self, piles):
n = len(piles)
dp = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
dp[i][i] == 0
for i in range(n-2,-1,-1):
for j in range(i+1,n):
dp[i][j] = max((piles[i]-dp[i+1][j]),(piles[j]-dp[i][j-1]))
return dp[0][n-1]>0
边栏推荐
猜你喜欢

03_ Linear table_ Linked list

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

How to find a sense of career direction

15_ Redis_ Redis. Conf detailed explanation

02_线性表_顺序表

05_ queue

04_ 栈

XML Configuration File

Build your own semantic segmentation platform deeplabv3+

Bing. Site Internet
随机推荐
Infra11199 database system
Solution of Queen n problem
How to write sensor data into computer database
Leetcode skimming -- incremental ternary subsequence 334 medium
Recommended configuration of tidb software and hardware environment
NBA player analysis
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
Force deduction solution summary 2029 stone game IX
05_队列
19_Redis_宕机后手动配置主机
Tidb hybrid deployment topology
搭建自己的语义分割平台deeplabV3+
16_ Redis_ Redis persistence
04_ Stack
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
yolo格式数据集处理(xml转txt)
Custom exception
工程师评测 | RK3568开发板上手测试
Common English abbreviations for data analysis (I)
LeetCode_ String_ Simple_ 412.Fizz Buzz