Skip to content

Commit 586bcc6

Browse files
committed
feat: add solutions to lc problem: No.0486
No.0486.Predict the Winner
1 parent 17411a2 commit 586bcc6

File tree

5 files changed

+335
-2
lines changed

5 files changed

+335
-2
lines changed

solution/0400-0499/0486.Predict the Winner/README.md

+139-1
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,160 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:记忆化搜索**
49+
50+
定义函数 `dfs(i, j)` 表示先手面对数组 `nums[i..j]` 时,能够获得的最大分数。
51+
52+
我们先看后手,后手可能面对的情况有两种,分别是 `nums[i+1..j]``nums[i..j-1]`,获得的最大分数分别为 `dfs(i+1, j)``dfs(i, j-1)`
53+
54+
先手要最大化自己的分数,就要让后手可获得的分数最小,即 `min(dfs(i+1, j), dfs(i, j-1))`。所以先手能获得的分数为 `sum(nums[i..j]) - min(dfs(i+1, j), dfs(i, j-1))`
55+
56+
记忆化搜索即可。
57+
58+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为数组 `nums` 的长度。
59+
4860
<!-- tabs:start -->
4961

5062
### **Python3**
5163

5264
<!-- 这里可写当前语言的特殊实现逻辑 -->
5365

5466
```python
55-
67+
class Solution:
68+
def PredictTheWinner(self, nums: List[int]) -> bool:
69+
@cache
70+
def dfs(i, j):
71+
if i > j:
72+
return 0
73+
a = min(dfs(i + 1, j), dfs(i, j - 1))
74+
return s[j + 1] - s[i] - a
75+
76+
s = list(accumulate(nums, initial=0))
77+
return dfs(0, len(nums) - 1) * 2 >= s[-1]
5678
```
5779

5880
### **Java**
5981

6082
<!-- 这里可写当前语言的特殊实现逻辑 -->
6183

6284
```java
85+
class Solution {
86+
public boolean PredictTheWinner(int[] nums) {
87+
int n = nums.length;
88+
if ((n & 1) == 0) {
89+
return true;
90+
}
91+
int[] f = new int[n];
92+
for (int i = n - 1; i >= 0; --i) {
93+
f[i] = nums[i];
94+
for (int j = i + 1; j < n; ++j) {
95+
f[j] = Math.max(nums[i] - f[j], nums[j] - f[j - 1]);
96+
}
97+
}
98+
return f[n - 1] >= 0;
99+
}
100+
}
101+
```
102+
103+
```java
104+
class Solution {
105+
private int[] s;
106+
private int[][] f;
107+
108+
public boolean PredictTheWinner(int[] nums) {
109+
int n = nums.length;
110+
s = new int[n + 1];
111+
for (int i = 0; i < n; ++i) {
112+
s[i + 1] = s[i] + nums[i];
113+
}
114+
f = new int[n + 1][n + 1];
115+
for (var e : f) {
116+
Arrays.fill(e, -1);
117+
}
118+
return dfs(0, n - 1) * 2 >= s[n];
119+
}
120+
121+
private int dfs(int i, int j) {
122+
if (i > j) {
123+
return 0;
124+
}
125+
if (f[i][j] != -1) {
126+
return f[i][j];
127+
}
128+
int a = Math.min(dfs(i + 1, j), dfs(i, j - 1));
129+
int res = s[j + 1] - s[i] - a;
130+
f[i][j] = res;
131+
return res;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<vector<int>> f;
142+
vector<int> s;
143+
144+
bool PredictTheWinner(vector<int>& nums) {
145+
int n = nums.size();
146+
s.resize(n + 1);
147+
for (int i = 0; i < n; ++i) {
148+
s[i + 1] = s[i] + nums[i];
149+
}
150+
f.assign(n + 1, vector<int>(n + 1, -1));
151+
return dfs(0, n - 1) * 2 >= s[n];
152+
}
153+
154+
int dfs(int i, int j) {
155+
if (i > j) return 0;
156+
if (f[i][j] != -1) return f[i][j];
157+
int a = min(dfs(i + 1, j), dfs(i, j - 1));
158+
int res = s[j + 1] - s[i] - a;
159+
f[i][j] = res;
160+
return res;
161+
}
162+
};
163+
```
63164
165+
### **Go**
166+
167+
```go
168+
func PredictTheWinner(nums []int) bool {
169+
n := len(nums)
170+
s := make([]int, n+1)
171+
f := make([][]int, n+1)
172+
for i, v := range nums {
173+
s[i+1] = s[i] + v
174+
}
175+
for i := range f {
176+
f[i] = make([]int, n+1)
177+
for j := range f[i] {
178+
f[i][j] = -1
179+
}
180+
}
181+
var dfs func(i, j int) int
182+
dfs = func(i, j int) int {
183+
if i > j {
184+
return 0
185+
}
186+
if f[i][j] != -1 {
187+
return f[i][j]
188+
}
189+
a := min(dfs(i+1, j), dfs(i, j-1))
190+
f[i][j] = s[j+1] - s[i] - a
191+
return f[i][j]
192+
}
193+
return dfs(0, n-1)*2 >= s[n]
194+
}
195+
196+
func min(a, b int) int {
197+
if a < b {
198+
return a
199+
}
200+
return b
201+
}
64202
```
65203

66204
### **...**

solution/0400-0499/0486.Predict the Winner/README_EN.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,139 @@ Finally, player 1 has more score (234) than player 2 (12), so you need to return
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def PredictTheWinner(self, nums: List[int]) -> bool:
51+
@cache
52+
def dfs(i, j):
53+
if i > j:
54+
return 0
55+
a = min(dfs(i + 1, j), dfs(i, j - 1))
56+
return s[j + 1] - s[i] - a
57+
58+
s = list(accumulate(nums, initial=0))
59+
return dfs(0, len(nums) - 1) * 2 >= s[-1]
5060
```
5161

5262
### **Java**
5363

5464
```java
65+
class Solution {
66+
public boolean PredictTheWinner(int[] nums) {
67+
int n = nums.length;
68+
if ((n & 1) == 0) {
69+
return true;
70+
}
71+
int[] f = new int[n];
72+
for (int i = n - 1; i >= 0; --i) {
73+
f[i] = nums[i];
74+
for (int j = i + 1; j < n; ++j) {
75+
f[j] = Math.max(nums[i] - f[j], nums[j] - f[j - 1]);
76+
}
77+
}
78+
return f[n - 1] >= 0;
79+
}
80+
}
81+
```
82+
83+
```java
84+
class Solution {
85+
private int[] s;
86+
private int[][] f;
87+
88+
public boolean PredictTheWinner(int[] nums) {
89+
int n = nums.length;
90+
s = new int[n + 1];
91+
for (int i = 0; i < n; ++i) {
92+
s[i + 1] = s[i] + nums[i];
93+
}
94+
f = new int[n + 1][n + 1];
95+
for (var e : f) {
96+
Arrays.fill(e, -1);
97+
}
98+
return dfs(0, n - 1) * 2 >= s[n];
99+
}
100+
101+
private int dfs(int i, int j) {
102+
if (i > j) {
103+
return 0;
104+
}
105+
if (f[i][j] != -1) {
106+
return f[i][j];
107+
}
108+
int a = Math.min(dfs(i + 1, j), dfs(i, j - 1));
109+
int res = s[j + 1] - s[i] - a;
110+
f[i][j] = res;
111+
return res;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
vector<vector<int>> f;
122+
vector<int> s;
123+
124+
bool PredictTheWinner(vector<int>& nums) {
125+
int n = nums.size();
126+
s.resize(n + 1);
127+
for (int i = 0; i < n; ++i) {
128+
s[i + 1] = s[i] + nums[i];
129+
}
130+
f.assign(n + 1, vector<int>(n + 1, -1));
131+
return dfs(0, n - 1) * 2 >= s[n];
132+
}
133+
134+
int dfs(int i, int j) {
135+
if (i > j) return 0;
136+
if (f[i][j] != -1) return f[i][j];
137+
int a = min(dfs(i + 1, j), dfs(i, j - 1));
138+
int res = s[j + 1] - s[i] - a;
139+
f[i][j] = res;
140+
return res;
141+
}
142+
};
143+
```
55144
145+
### **Go**
146+
147+
```go
148+
func PredictTheWinner(nums []int) bool {
149+
n := len(nums)
150+
s := make([]int, n+1)
151+
f := make([][]int, n+1)
152+
for i, v := range nums {
153+
s[i+1] = s[i] + v
154+
}
155+
for i := range f {
156+
f[i] = make([]int, n+1)
157+
for j := range f[i] {
158+
f[i][j] = -1
159+
}
160+
}
161+
var dfs func(i, j int) int
162+
dfs = func(i, j int) int {
163+
if i > j {
164+
return 0
165+
}
166+
if f[i][j] != -1 {
167+
return f[i][j]
168+
}
169+
a := min(dfs(i+1, j), dfs(i, j-1))
170+
f[i][j] = s[j+1] - s[i] - a
171+
return f[i][j]
172+
}
173+
return dfs(0, n-1)*2 >= s[n]
174+
}
175+
176+
func min(a, b int) int {
177+
if a < b {
178+
return a
179+
}
180+
return b
181+
}
56182
```
57183

58184
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> f;
4+
vector<int> s;
5+
6+
bool PredictTheWinner(vector<int>& nums) {
7+
int n = nums.size();
8+
s.resize(n + 1);
9+
for (int i = 0; i < n; ++i) {
10+
s[i + 1] = s[i] + nums[i];
11+
}
12+
f.assign(n + 1, vector<int>(n + 1, -1));
13+
return dfs(0, n - 1) * 2 >= s[n];
14+
}
15+
16+
int dfs(int i, int j) {
17+
if (i > j) return 0;
18+
if (f[i][j] != -1) return f[i][j];
19+
int a = min(dfs(i + 1, j), dfs(i, j - 1));
20+
int res = s[j + 1] - s[i] - a;
21+
f[i][j] = res;
22+
return res;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func PredictTheWinner(nums []int) bool {
2+
n := len(nums)
3+
s := make([]int, n+1)
4+
f := make([][]int, n+1)
5+
for i, v := range nums {
6+
s[i+1] = s[i] + v
7+
}
8+
for i := range f {
9+
f[i] = make([]int, n+1)
10+
for j := range f[i] {
11+
f[i][j] = -1
12+
}
13+
}
14+
var dfs func(i, j int) int
15+
dfs = func(i, j int) int {
16+
if i > j {
17+
return 0
18+
}
19+
if f[i][j] != -1 {
20+
return f[i][j]
21+
}
22+
a := min(dfs(i+1, j), dfs(i, j-1))
23+
f[i][j] = s[j+1] - s[i] - a
24+
return f[i][j]
25+
}
26+
return dfs(0, n-1)*2 >= s[n]
27+
}
28+
29+
func min(a, b int) int {
30+
if a < b {
31+
return a
32+
}
33+
return b
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def PredictTheWinner(self, nums: List[int]) -> bool:
3+
@cache
4+
def dfs(i, j):
5+
if i > j:
6+
return 0
7+
a = min(dfs(i + 1, j), dfs(i, j - 1))
8+
return s[j + 1] - s[i] - a
9+
10+
s = list(accumulate(nums, initial=0))
11+
return dfs(0, len(nums) - 1) * 2 >= s[-1]

0 commit comments

Comments
 (0)