Skip to content

Commit fd32275

Browse files
committed
+ problem 486
1 parent 780ee39 commit fd32275

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 486. Predict the Winner
2+
You are given an integer array `nums`. Two players are playing a game with this array: player 1 and player 2.
3+
4+
Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of `0`. At each turn, the player takes one of the numbers from either end of the array (i.e., `nums[0]` or `nums[nums.length - 1]`) which reduces the size of the array by `1`. The player adds the chosen number to their score. The game ends when there are no more elements in the array.
5+
6+
Return `true` if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return `true`. You may assume that both players are playing optimally.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> nums = [1,5,2]
11+
<strong>Output:</strong> false
12+
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
13+
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
14+
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
15+
Hence, player 1 will never be the winner and you need to return false.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> nums = [1,5,233,7]
21+
<strong>Output:</strong> true
22+
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
23+
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
24+
</pre>
25+
26+
#### Constraints:
27+
* `1 <= nums.length <= 20`
28+
* <code>0 <= nums[i] <= 10<sup>7</sup></code>
29+
30+
## Solutions (Python)
31+
32+
### 1. Solution
33+
```Python
34+
from functools import cache
35+
36+
37+
class Solution:
38+
def predictTheWinner(self, nums: List[int]) -> bool:
39+
@cache
40+
def subArrayMaxDiff(i: int, j: int) -> int:
41+
if i == j:
42+
return nums[i]
43+
44+
return max(nums[i] - subArrayMaxDiff(i + 1, j), nums[j] - subArrayMaxDiff(i, j - 1))
45+
46+
return subArrayMaxDiff(0, len(nums) - 1) >= 0
47+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 486. 预测赢家
2+
给你一个整数数组 `nums` 。玩家 1 和玩家 2 基于这个数组设计了一个游戏。
3+
4+
玩家 1 和玩家 2 轮流进行自己的回合,玩家 1 先手。开始时,两个玩家的初始分值都是 `0` 。每一回合,玩家从数组的任意一端取一个数字(即,`nums[0]``nums[nums.length - 1]`),取到的数字将会从数组中移除(数组长度减 `1` )。玩家选中的数字将会加到他的得分上。当数组中没有剩余数字可取时,游戏结束。
5+
6+
如果玩家 1 能成为赢家,返回 `true` 。如果两个玩家得分相等,同样认为玩家 1 是游戏的赢家,也返回 `true` 。你可以假设每个玩家的玩法都会使他的分数最大化。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> nums = [1,5,2]
11+
<strong>输出:</strong> false
12+
<strong>解释:</strong> 一开始,玩家 1 可以从 1 和 2 中进行选择。
13+
如果他选择 2(或者 1 ),那么玩家 2 可以从 1(或者 2 )和 5 中进行选择。如果玩家 2 选择了 5 ,那么玩家 1 则只剩下 1(或者 2 )可选。
14+
所以,玩家 1 的最终分数为 1 + 2 = 3,而玩家 2 为 5 。
15+
因此,玩家 1 永远不会成为赢家,返回 false 。
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> nums = [1,5,233,7]
21+
<strong>输出:</strong> true
22+
<strong>解释:</strong> 玩家 1 一开始选择 1 。然后玩家 2 必须从 5 和 7 中进行选择。无论玩家 2 选择了哪个,玩家 1 都可以选择 233 。
23+
最终,玩家 1(234 分)比玩家 2(12 分)获得更多的分数,所以返回 true,表示玩家 1 可以成为赢家。
24+
</pre>
25+
26+
#### 提示:
27+
* `1 <= nums.length <= 20`
28+
* <code>0 <= nums[i] <= 10<sup>7</sup></code>
29+
30+
## 题解 (Python)
31+
32+
### 1. 题解
33+
```Python
34+
from functools import cache
35+
36+
37+
class Solution:
38+
def predictTheWinner(self, nums: List[int]) -> bool:
39+
@cache
40+
def subArrayMaxDiff(i: int, j: int) -> int:
41+
if i == j:
42+
return nums[i]
43+
44+
return max(nums[i] - subArrayMaxDiff(i + 1, j), nums[j] - subArrayMaxDiff(i, j - 1))
45+
46+
return subArrayMaxDiff(0, len(nums) - 1) >= 0
47+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def predictTheWinner(self, nums: List[int]) -> bool:
6+
@cache
7+
def subArrayMaxDiff(i: int, j: int) -> int:
8+
if i == j:
9+
return nums[i]
10+
11+
return max(nums[i] - subArrayMaxDiff(i + 1, j), nums[j] - subArrayMaxDiff(i, j - 1))
12+
13+
return subArrayMaxDiff(0, len(nums) - 1) >= 0

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@
349349
[482][482l] |[License Key Formatting][482] |![py]
350350
[483][483l] |[Smallest Good Base][483] |![py]
351351
[485][485l] |[Max Consecutive Ones][485] |![rs]
352+
[486][486l] |[Predict the Winner][486] |![py]
352353
[488][488l] |[Zuma Game][488] |![py]
353354
[492][492l] |[Construct the Rectangle][492] |![rs]
354355
[493][493l] |[Reverse Pairs][493] |![py]
@@ -2061,6 +2062,7 @@
20612062
[482]:Problemset/0482-License%20Key%20Formatting/README.md#482-license-key-formatting
20622063
[483]:Problemset/0483-Smallest%20Good%20Base/README.md#483-smallest-good-base
20632064
[485]:Problemset/0485-Max%20Consecutive%20Ones/README.md#485-max-consecutive-ones
2065+
[486]:Problemset/0486-Predict%20the%20Winner/README.md#486-predict-the-winner
20642066
[488]:Problemset/0488-Zuma%20Game/README.md#488-zuma-game
20652067
[492]:Problemset/0492-Construct%20the%20Rectangle/README.md#492-construct-the-rectangle
20662068
[493]:Problemset/0493-Reverse%20Pairs/README.md#493-reverse-pairs
@@ -3767,6 +3769,7 @@
37673769
[482l]:https://leetcode.com/problems/license-key-formatting/
37683770
[483l]:https://leetcode.com/problems/smallest-good-base/
37693771
[485l]:https://leetcode.com/problems/max-consecutive-ones/
3772+
[486l]:https://leetcode.com/problems/predict-the-winner/
37703773
[488l]:https://leetcode.com/problems/zuma-game/
37713774
[492l]:https://leetcode.com/problems/construct-the-rectangle/
37723775
[493l]:https://leetcode.com/problems/reverse-pairs/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@
349349
[482][482l] |[密钥格式化][482] |![py]
350350
[483][483l] |[最小好进制][483] |![py]
351351
[485][485l] |[最大连续1的个数][485] |![rs]
352+
[486][486l] |[预测赢家][486] |![py]
352353
[488][488l] |[祖玛游戏][488] |![py]
353354
[492][492l] |[构造矩形][492] |![rs]
354355
[493][493l] |[翻转对][493] |![py]
@@ -2061,6 +2062,7 @@
20612062
[482]:Problemset/0482-License%20Key%20Formatting/README_CN.md#482-密钥格式化
20622063
[483]:Problemset/0483-Smallest%20Good%20Base/README_CN.md#483-最小好进制
20632064
[485]:Problemset/0485-Max%20Consecutive%20Ones/README_CN.md#485-最大连续1的个数
2065+
[486]:Problemset/0486-Predict%20the%20Winner/README_CN.md#486-预测赢家
20642066
[488]:Problemset/0488-Zuma%20Game/README_CN.md#488-祖玛游戏
20652067
[492]:Problemset/0492-Construct%20the%20Rectangle/README_CN.md#492-构造矩形
20662068
[493]:Problemset/0493-Reverse%20Pairs/README_CN.md#493-翻转对
@@ -3767,6 +3769,7 @@
37673769
[482l]:https://leetcode.cn/problems/license-key-formatting/
37683770
[483l]:https://leetcode.cn/problems/smallest-good-base/
37693771
[485l]:https://leetcode.cn/problems/max-consecutive-ones/
3772+
[486l]:https://leetcode.cn/problems/predict-the-winner/
37703773
[488l]:https://leetcode.cn/problems/zuma-game/
37713774
[492l]:https://leetcode.cn/problems/construct-the-rectangle/
37723775
[493l]:https://leetcode.cn/problems/reverse-pairs/

0 commit comments

Comments
 (0)