当前位置:网站首页>[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
边栏推荐
- 【LeetCode】876-链表的中间结点
- [solution] educational codeforces round 82
- 党史纪实主题公益数字文创产品正式上线
- MySQL -- Index Optimization -- order by
- 高考录取分数线爬取
- 微信支付宝账户体系和支付接口业务流程
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- Bing. Site Internet
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- Force deduction solution summarizes the lucky numbers in 1380 matrix
猜你喜欢
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
SQL transaction
Pytoch saves tensor to Mat file
Leetcode skimming - remove duplicate letters 316 medium
04_ Stack
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
怎样从微信返回的json字符串中截取某个key的值?
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
随机推荐
04_ 栈
12_Redis_Bitmap_命令
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
17_ Redis_ Redis publish subscription
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
LeetCode刷题——奇偶链表#328#Medium
Evaluation of embedded rz/g2l processor core board and development board of Feiling
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
05_ queue
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
6.12 critical moment of Unified Process Platform
Force deduction solution summary 2029 stone game IX
Leetcode skimming -- incremental ternary subsequence 334 medium
【LeetCode】977-有序数组的平方
21_Redis_浅析Redis缓存穿透和雪崩
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Basic knowledge of cryptography
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
Pytorch 保存tensor到.mat文件
How does the computer set up speakers to play microphone sound