Skip to content

Commit adb80ec

Browse files
committed
+ problem 1140
1 parent d4bcfb5 commit adb80ec

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 1140. Stone Game II
2+
Alice and Bob continue their games with piles of stones. There are a number of piles **arranged in a row**, and each pile has a positive integer number of stones `piles[i]`. The objective of the game is to end with the most stones.
3+
4+
Alice and Bob take turns, with Alice starting first.
5+
6+
On each player's turn, that player can take **all the stones** in the **first** X remaining piles, where `1 <= X <= 2M`. Then, we set `M = max(M, X)`. Initially, M = 1.
7+
8+
The game continues until all the stones have been taken.
9+
10+
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> piles = [2,7,9,4,4]
15+
<strong>Output:</strong> 10
16+
<strong>Explanation:</strong>
17+
* If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get 2 + 4 + 4 = 10 stones in total.
18+
* If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 stones in total.
19+
So we return 10 since it's larger.
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> piles = [1,2,3,4,5,100]
25+
<strong>Output:</strong> 104
26+
</pre>
27+
28+
#### Constraints:
29+
* `1 <= piles.length <= 100`
30+
* <code>1 <= piles[i] <= 10<sup>4</sup></code>
31+
32+
## Solutions (Python)
33+
34+
### 1. Solution
35+
```Python
36+
from functools import cache
37+
38+
39+
class Solution:
40+
def stoneGameII(self, piles: List[int]) -> int:
41+
@cache
42+
def subGame(m: int, i: int) -> int:
43+
if i == len(piles):
44+
return 0
45+
46+
total = sum(piles[i:])
47+
48+
return total - min(subGame(max(m, x), i + x) for x in range(1, min(2 * m, len(piles) - i) + 1))
49+
50+
return subGame(1, 0)
51+
```
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 1140. 石子游戏 II
2+
Alice 和 Bob 继续他们的石子游戏。许多堆石子 **排成一行**,每堆都有正整数颗石子 `piles[i]`。游戏以谁手中的石子最多来决出胜负。
3+
4+
Alice 和 Bob 轮流进行,Alice 先开始。最初,`M = 1`
5+
6+
在每个玩家的回合中,该玩家可以拿走剩下的 **** `X` 堆的所有石子,其中 `1 <= X <= 2M`。然后,令 `M = max(M, X)`
7+
8+
游戏一直持续到所有石子都被拿走。
9+
10+
假设 Alice 和 Bob 都发挥出最佳水平,返回 Alice 可以得到的最大数量的石头。
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> piles = [2,7,9,4,4]
15+
<strong>输出:</strong> 10
16+
<strong>解释:</strong> 如果一开始 Alice 取了一堆,Bob 取了两堆,然后 Alice 再取两堆。Alice 可以得到 2 + 4 + 4 = 10 堆。
17+
如果 Alice 一开始拿走了两堆,那么 Bob 可以拿走剩下的三堆。在这种情况下,Alice 得到 2 + 7 = 9 堆。返回 10,因为它更大。
18+
</pre>
19+
20+
#### 示例 2:
21+
<pre>
22+
<strong>输入:</strong> piles = [1,2,3,4,5,100]
23+
<strong>输出:</strong> 104
24+
</pre>
25+
26+
#### 提示:
27+
* `1 <= piles.length <= 100`
28+
* <code>1 <= piles[i] <= 10<sup>4</sup></code>
29+
30+
## 题解 (Python)
31+
32+
### 1. 题解
33+
```Python
34+
from functools import cache
35+
36+
37+
class Solution:
38+
def stoneGameII(self, piles: List[int]) -> int:
39+
@cache
40+
def subGame(m: int, i: int) -> int:
41+
if i == len(piles):
42+
return 0
43+
44+
total = sum(piles[i:])
45+
46+
return total - min(subGame(max(m, x), i + x) for x in range(1, min(2 * m, len(piles) - i) + 1))
47+
48+
return subGame(1, 0)
49+
```
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def stoneGameII(self, piles: List[int]) -> int:
6+
@cache
7+
def subGame(m: int, i: int) -> int:
8+
if i == len(piles):
9+
return 0
10+
11+
total = sum(piles[i:])
12+
13+
return total - min(subGame(max(m, x), i + x) for x in range(1, min(2 * m, len(piles) - i) + 1))
14+
15+
return subGame(1, 0)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@
790790
[1137][1137l]|[N-th Tribonacci Number][1137] |![rs]
791791
[1138][1138l]|[Alphabet Board Path][1138] |![rb]&nbsp;&nbsp;![rs]
792792
[1139][1139l]|[Largest 1-Bordered Square][1139] |![rs]
793+
[1140][1140l]|[Stone Game II][1140] |![py]
793794
[1143][1143l]|[Longest Common Subsequence][1143] |![rs]
794795
[1144][1144l]|[Decrease Elements To Make Array Zigzag][1144] |![rs]
795796
[1145][1145l]|[Binary Tree Coloring Game][1145] |![py]
@@ -2500,6 +2501,7 @@
25002501
[1137]:Problemset/1137-N-th%20Tribonacci%20Number/README.md#1137-n-th-tribonacci-number
25012502
[1138]:Problemset/1138-Alphabet%20Board%20Path/README.md#1138-alphabet-board-path
25022503
[1139]:Problemset/1139-Largest%201-Bordered%20Square/README.md#1139-largest-1-bordered-square
2504+
[1140]:Problemset/1140-Stone%20Game%20II/README.md#1140-stone-game-ii
25032505
[1143]:Problemset/1143-Longest%20Common%20Subsequence/README.md#1143-longest-common-subsequence
25042506
[1144]:Problemset/1144-Decrease%20Elements%20To%20Make%20Array%20Zigzag/README.md#1144-decrease-elements-to-make-array-zigzag
25052507
[1145]:Problemset/1145-Binary%20Tree%20Coloring%20Game/README.md#1145-binary-tree-coloring-game
@@ -4204,6 +4206,7 @@
42044206
[1137l]:https://leetcode.com/problems/n-th-tribonacci-number/
42054207
[1138l]:https://leetcode.com/problems/alphabet-board-path/
42064208
[1139l]:https://leetcode.com/problems/largest-1-bordered-square/
4209+
[1140l]:https://leetcode.com/problems/stone-game-ii/
42074210
[1143l]:https://leetcode.com/problems/longest-common-subsequence/
42084211
[1144l]:https://leetcode.com/problems/decrease-elements-to-make-array-zigzag/
42094212
[1145l]:https://leetcode.com/problems/binary-tree-coloring-game/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@
790790
[1137][1137l]|[第 N 个泰波那契数][1137] |![rs]
791791
[1138][1138l]|[字母板上的路径][1138] |![rb]&nbsp;&nbsp;![rs]
792792
[1139][1139l]|[最大的以 1 为边界的正方形][1139] |![rs]
793+
[1140][1140l]|[石子游戏 II][1140] |![py]
793794
[1143][1143l]|[最长公共子序列][1143] |![rs]
794795
[1144][1144l]|[递减元素使数组呈锯齿状][1144] |![rs]
795796
[1145][1145l]|[二叉树着色游戏][1145] |![py]
@@ -2500,6 +2501,7 @@
25002501
[1137]:Problemset/1137-N-th%20Tribonacci%20Number/README_CN.md#1137-第-n-个泰波那契数
25012502
[1138]:Problemset/1138-Alphabet%20Board%20Path/README_CN.md#1138-字母板上的路径
25022503
[1139]:Problemset/1139-Largest%201-Bordered%20Square/README_CN.md#1139-最大的以-1-为边界的正方形
2504+
[1140]:Problemset/1140-Stone%20Game%20II/README_CN.md#1140-石子游戏-ii
25032505
[1143]:Problemset/1143-Longest%20Common%20Subsequence/README_CN.md#1143-最长公共子序列
25042506
[1144]:Problemset/1144-Decrease%20Elements%20To%20Make%20Array%20Zigzag/README_CN.md#1144-递减元素使数组呈锯齿状
25052507
[1145]:Problemset/1145-Binary%20Tree%20Coloring%20Game/README_CN.md#1145-二叉树着色游戏
@@ -4204,6 +4206,7 @@
42044206
[1137l]:https://leetcode.cn/problems/n-th-tribonacci-number/
42054207
[1138l]:https://leetcode.cn/problems/alphabet-board-path/
42064208
[1139l]:https://leetcode.cn/problems/largest-1-bordered-square/
4209+
[1140l]:https://leetcode.cn/problems/stone-game-ii/
42074210
[1143l]:https://leetcode.cn/problems/longest-common-subsequence/
42084211
[1144l]:https://leetcode.cn/problems/decrease-elements-to-make-array-zigzag/
42094212
[1145l]:https://leetcode.cn/problems/binary-tree-coloring-game/

0 commit comments

Comments
 (0)