当前位置:网站首页>【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
边栏推荐
- 11_ Redis_ Hyperloglog_ command
- 党史纪实主题公益数字文创产品正式上线
- 你不知道的Set集合
- Leetcode skimming - remove duplicate letters 316 medium
- LeetCode刷题——两整数之和#371#Medium
- Download blender on Alibaba cloud image station
- 【LeetCode】417-太平洋大西洋水流问题
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
猜你喜欢

15_Redis_Redis.conf详解

XML Configuration File

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

18_Redis_Redis主从复制&&集群搭建

List set & UML diagram

How to intercept the value of a key from the JSON string returned by wechat?

Beijing rental data analysis

How to avoid 7 common problems in mobile and network availability testing

工程师评测 | RK3568开发板上手测试

彻底弄懂浏览器强缓存和协商缓存
随机推荐
Pytoch saves tensor to Mat file
05_ queue
13_ Redis_ affair
【LeetCode】1162-地图分析
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
6.12 critical moment of Unified Process Platform
Custom exception
08_ strand
18_Redis_Redis主从复制&&集群搭建
14_Redis_乐观锁
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
I made an istio workshop. This is the first introduction
How to test tidb with sysbench
【LeetCode】876-链表的中间结点
Party History Documentary theme public welfare digital cultural and creative products officially launched
Map introduction
18_ Redis_ Redis master-slave replication & cluster building
12_Redis_Bitmap_命令
LeetCode刷题——去除重复字母#316#Medium
Summary of the first three passes of sqli Labs