当前位置:网站首页>[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/
边栏推荐
- MD5 encryption
- LeetCode刷题——两整数之和#371#Medium
- folium地图无法显示的问题,临时性解决方案如下
- 【LeetCode】577-反转字符串中的单词 III
- Practical debugging skills
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- How to find a sense of career direction
- [leetcode] 167 - sum of two numbers II - enter an ordered array
- 【LeetCode】876-链表的中间结点
- Common English abbreviations for data analysis (I)
猜你喜欢

【LeetCode】417-太平洋大西洋水流问题

Leetcode skimming -- count the number of numbers with different numbers 357 medium

FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)

10_ Redis_ geospatial_ command

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

(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

党史纪实主题公益数字文创产品正式上线

Oracle primary key auto increment

Leetcode skimming -- sum of two integers 371 medium
随机推荐
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Real estate market trend outlook in 2022
How to find a sense of career direction
LeetCode刷题——奇偶链表#328#Medium
17_Redis_Redis发布订阅
Party History Documentary theme public welfare digital cultural and creative products officially launched
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
[network security] network asset collection
PTA 天梯赛习题集 L2-001 城市间紧急救援
【LeetCode】1140-石子游戏II
【LeetCode】344-反转字符串
04_ Stack
16_ Redis_ Redis persistence
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Infra11199 database system
14_Redis_乐观锁
士官类学校名录
【LeetCode】1162-地图分析
11_Redis_Hyperloglog_命令
17_ Redis_ Redis publish subscription