Skip to content

Commit 757f45f

Browse files
authored
feat: add rust solution to lc problem: No.0486 (#1330)
1 parent 858304d commit 757f45f

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,37 @@ public:
201201
};
202202
```
203203

204+
### **Rust**
205+
206+
```rust
207+
impl Solution {
208+
#[allow(dead_code)]
209+
pub fn predict_the_winner(nums: Vec<i32>) -> bool {
210+
let n = nums.len();
211+
let mut dp: Vec<Vec<i32>> = vec![vec![0; n]; n];
212+
213+
// Initialize the dp vector
214+
for i in 0..n {
215+
dp[i][i] = nums[i];
216+
}
217+
218+
// Begin the dp process
219+
for i in (0..n - 1).rev() {
220+
for j in (i + 1)..n {
221+
dp[i][j] = std::cmp::max(
222+
// Take i-th num
223+
nums[i] - dp[i + 1][j],
224+
// Take j-th num
225+
nums[j] - dp[i][j - 1],
226+
);
227+
}
228+
}
229+
230+
dp[0][n - 1] >= 0
231+
}
232+
}
233+
```
234+
204235
### **Go**
205236

206237
```go

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

+31
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,37 @@ public:
157157
};
158158
```
159159

160+
### **Rust**
161+
162+
```rust
163+
impl Solution {
164+
#[allow(dead_code)]
165+
pub fn predict_the_winner(nums: Vec<i32>) -> bool {
166+
let n = nums.len();
167+
let mut dp: Vec<Vec<i32>> = vec![vec![0; n]; n];
168+
169+
// Initialize the dp vector
170+
for i in 0..n {
171+
dp[i][i] = nums[i];
172+
}
173+
174+
// Begin the dp process
175+
for i in (0..n - 1).rev() {
176+
for j in (i + 1)..n {
177+
dp[i][j] = std::cmp::max(
178+
// Take i-th num
179+
nums[i] - dp[i + 1][j],
180+
// Take j-th num
181+
nums[j] - dp[i][j - 1],
182+
);
183+
}
184+
}
185+
186+
dp[0][n - 1] >= 0
187+
}
188+
}
189+
```
190+
160191
### **Go**
161192

162193
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn predict_the_winner(nums: Vec<i32>) -> bool {
4+
let n = nums.len();
5+
let mut dp: Vec<Vec<i32>> = vec![vec![0; n]; n];
6+
7+
// Initialize the dp vector
8+
for i in 0..n {
9+
dp[i][i] = nums[i];
10+
}
11+
12+
// Begin the dp process
13+
for i in (0..n - 1).rev() {
14+
for j in (i + 1)..n {
15+
dp[i][j] = std::cmp::max(
16+
// Take i-th num
17+
nums[i] - dp[i + 1][j],
18+
// Take j-th num
19+
nums[j] - dp[i][j - 1],
20+
);
21+
}
22+
}
23+
24+
dp[0][n - 1] >= 0
25+
}
26+
}

0 commit comments

Comments
 (0)