当前位置:网站首页>[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/
边栏推荐
- Summary of the first three passes of sqli Labs
- 【Leetcode】167-两数之和II -输入有序数组
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- [leetcode] 167 - sum of two numbers II - enter an ordered array
- 5. Practice: jctree implements the annotation processor at compile time
- 14_ Redis_ Optimistic lock
- Steps for Navicat to create a new database
- 彻底弄懂浏览器强缓存和协商缓存
- yolo格式数据集处理(xml转txt)
- 【LeetCode】695-岛屿的最大面积
猜你喜欢
02_ Linear table_ Sequence table
Redux - detailed explanation
Yolo format data set processing (XML to txt)
03.golang初步使用
17_ Redis_ Redis publish subscription
Markdown tutorial
Leetcode skimming -- incremental ternary subsequence 334 medium
03. Preliminary use of golang
Case introduction and problem analysis of microservice
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
随机推荐
Redux——详解
4. Data splitting of Flink real-time project
05_ queue
21_ Redis_ Analysis of redis cache penetration and avalanche
密码学基础知识
17_ Redis_ Redis publish subscription
【LeetCode】1140-石子游戏II
03_線性錶_鏈錶
Basic knowledge of cryptography
. Solution to the problem of Chinese garbled code when net core reads files
MySQL -- Index Optimization -- order by
20_ Redis_ Sentinel mode
JVM architecture, classloader, parental delegation mechanism
How to avoid 7 common problems in mobile and network availability testing
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
Bing. Site Internet
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Build your own semantic segmentation platform deeplabv3+
工程师评测 | RK3568开发板上手测试