当前位置:网站首页>[leetcode] 877 stone game
[leetcode] 877 stone game
2022-07-02 15:36:00 【Crisp ~】
Alice and Bob Playing games with piles of stones . There are even piles of stones , Line up ; Every pile has just A whole stone , The number is piles[i] .
Who has the most stones to decide the outcome of the game . Stony total yes Odd number , So there was no draw .
Alice and Bob Take turns ,Alice Let's start with . Every round , Players from the line Start or end Take away the whole pile of stones from . This continued until there were no more piles of stones , At this time, the hand Most stones The player win victory .
hypothesis Alice and Bob All at their best , When Alice Return when you win the game true , When Bob Return when you win the game false .
Example 1:
Input : piles = [5,3,4,5]
Output : true
explain :
Alice Let's start with , Only the front 5 Or after 5 Stone .
Suppose he takes the front 5 star , This line becomes [3,4,5] .
If Bob Before taking it away 3 star , So the rest is [4,5],Alice After taking it away 5 Win 10 branch .
If Bob After taking it away 5 star , So the rest is [3,4],Alice After taking it away 4 Win 9 branch .
This shows that , Take before 5 A stone is right Alice It's a victory move , So back true .
Example 2:
Input : piles = [3,7,2,3]
Output : true
Tips :
- 2 <= piles.length <= 500
- piles.length yes even numbers
- 1 <= piles[i] <= 500
- sum(piles[i]) yes Odd number
Ideas
reference Predicting Winners A question
# Dynamic programming
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
边栏推荐
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- [solution] educational codeforces round 82
- 04.进入云原生后的企业级应用构建的一些思考
- [network security] network asset collection
- MD5加密
- 14_Redis_乐观锁
- 10_Redis_geospatial_命令
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- Custom exception
- 微信支付宝账户体系和支付接口业务流程
猜你喜欢

微信支付宝账户体系和支付接口业务流程

Pytorch 保存tensor到.mat文件

03_ Linear table_ Linked list

19_ Redis_ Manually configure the host after downtime

Yolov5 code reproduction and server operation

PTA 天梯赛习题集 L2-001 城市间紧急救援

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

03_線性錶_鏈錶

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
随机推荐
Wechat Alipay account system and payment interface business process
语义分割学习笔记(一)
你不知道的Set集合
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
Engineer evaluation | rk3568 development board hands-on test
【LeetCode】877-石子游戏
08_ strand
Leetcode skimming - remove duplicate letters 316 medium
02. After containerization, you must face golang
07_ Hash
02_ Linear table_ Sequence table
Markdown tutorial
【LeetCode】577-反转字符串中的单词 III
【LeetCode】695-岛屿的最大面积
How does the computer set up speakers to play microphone sound
Semantic segmentation learning notes (1)
提前批院校名称
Common English abbreviations for data analysis (I)
yolo格式数据集处理(xml转txt)
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测