当前位置:网站首页>[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
边栏推荐
- 12_ Redis_ Bitmap_ command
- Solution of Queen n problem
- folium,确诊和密接轨迹上图
- How to write sensor data into computer database
- 11_Redis_Hyperloglog_命令
- 【LeetCode】1905-统计子岛屿
- Yolo format data set processing (XML to txt)
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- Infra11199 database system
- 搭建自己的语义分割平台deeplabV3+
猜你喜欢

Application and practice of Jenkins pipeline

21_ Redis_ Analysis of redis cache penetration and avalanche

03_線性錶_鏈錶

Download blender on Alibaba cloud image station

JVM architecture, classloader, parental delegation mechanism

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

17_ Redis_ Redis publish subscription

Redux - detailed explanation

语义分割学习笔记(一)

2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
随机推荐
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
Application of CDN in game field
语义分割学习笔记(一)
05_ queue
13_Redis_事务
MD5加密
怎样从微信返回的json字符串中截取某个key的值?
04_ 栈
高考分数线爬取
Pytoch saves tensor to Mat file
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
LeetCode刷题——两整数之和#371#Medium
损失函数与正负样本分配:YOLO系列
LeetCode刷题——去除重复字母#316#Medium
21_Redis_浅析Redis缓存穿透和雪崩
Download blender on Alibaba cloud image station
Bing. Com website
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
folium,确诊和密接轨迹上图
【LeetCode】577-反转字符串中的单词 III