当前位置:网站首页>[leetcode] 1140 stone game II
[leetcode] 1140 stone game II
2022-07-02 15:37:00 【Crisp ~】
Alice and Bob continue their stone game . Many heaps of stones Line up , Every pile has a whole number of stones piles[i]. Who has the most stones to decide the outcome of the game .
Alice and Bob take turns , Alice starts first . first ,M = 1.
In each player's turn , The player can take the rest front X All the stones in the pile , among 1 <= X <= 2M. then , Make M = max(M, X).
The game continued until all the stones were taken away .
Suppose Alice and Bob are at their best , Return to the maximum number of stones Alice can get .
Example 1:
Input : piles = [2,7,9,4,4]
Output : 10
explain : If at first Alice Took a pile ,Bob Took two piles , then Alice Take two more piles . Alice can get 2 + 4 + 4 = 10 Pile up . If Alice At first, I took away two piles , that Bob You can take away the remaining three piles . under these circumstances ,Alice obtain 2 + 7 = 9 Pile up . return 10, Because it's bigger .
Example 2:
Input : piles = [1,2,3,4,5,100]
Output : 104
Tips :
- 1 <= piles.length <= 100
- 1 <= piles[i] <= 104
Ideas
dp[M][i] Indicates that the remaining stone pile is piles[i : len] when ,M = M Under the circumstances , The maximum number of stones a first mover can get
- i + 2M >= length, dp[M][i] = sum(piles[i : length]), The remaining piles can be taken away directly
- i + 2M < length, dp[M][i] = max(dp[M][i],sum(piles[i : length]) - dp[max(M, X)][i + X]), among 1 <= X <= 2M, You can't take all the remaining piles away , Then the best situation is to let the next person take less . For all at this time x Value , The next person from X Start picking up ,M Turn into max(M, X), So the next person can take dp[max(M, X)][i + X], The best thing you can do is sum[i : len - 1] - dp[max(M, X)][i + X]
# Dynamic programming
class Solution(object):
def stoneGameII(self, piles):
length = len(piles)
mysum = 0
dp = [[0 for i in range(length)] for j in range(length+1)]
for i in range(length-1,-1,-1):
mysum += piles[i]
for M in range(1,length+1):
if i+2*M>=length:
dp[M][i]=mysum
else:
for X in range(1,2*M+1):
dp[M][i] = max(dp[M][i],mysum-dp[max(M,X)][i+X])
return dp[0][1]
Reference article :https://leetcode.cn/problems/stone-game-ii/solution/java-dong-tai-gui-hua-qing-xi-yi-dong-17xing-by-lg/
边栏推荐
- 04. Some thoughts on enterprise application construction after entering cloud native
- 【LeetCode】577-反转字符串中的单词 III
- Leetcode question brushing - parity linked list 328 medium
- How to intercept the value of a key from the JSON string returned by wechat?
- Bing.com網站
- 夏季高考文化成绩一分一段表
- SQL stored procedure
- 高考分数线爬取
- 03.golang初步使用
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
猜你喜欢

How does the computer set up speakers to play microphone sound

Evaluation of embedded rz/g2l processor core board and development board of Feiling

密码学基础知识

Yolo format data set processing (XML to txt)

Data analysis thinking analysis methods and business knowledge - business indicators

Leetcode question brushing - parity linked list 328 medium

Map introduction
![[leetcode] 1254 - count the number of closed Islands](/img/84/f888ae0e164951cd9623fb3bf4a984.png)
[leetcode] 1254 - count the number of closed Islands

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

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
随机推荐
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Case introduction and problem analysis of microservice
[network security] network asset collection
Custom exception
21_ Redis_ Analysis of redis cache penetration and avalanche
10_Redis_geospatial_命令
Build your own semantic segmentation platform deeplabv3+
XML Configuration File
高考录取分数线爬取
6.12 critical moment of Unified Process Platform
[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)
Party History Documentary theme public welfare digital cultural and creative products officially launched
MD5加密
04.进入云原生后的企业级应用构建的一些思考
16_Redis_Redis持久化
How to intercept the value of a key from the JSON string returned by wechat?
【LeetCode】486-预测赢家
13_ Redis_ affair
08_ strand