-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
47 lines (40 loc) · 1.14 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pub struct Solution {}
impl Solution {
pub fn stone_game_ii(piles: Vec<i32>) -> i32 {
let length = piles.len();
let mut dp_table = vec![vec![0; length+1]; length];
let mut suffix_sum = vec![0; length];
suffix_sum[length - 1] = piles[length - 1];
for i in (0..length - 1).rev() {
suffix_sum[i] = suffix_sum[i + 1] + piles[i];
}
for i in (0..length).rev() {
for m in 1..=length {
if i + 2 * m >= length {
dp_table[i][m] = suffix_sum[i];
}
else {
for x in 1..=2*m{
dp_table[i][m] = dp_table[i][m].max(suffix_sum[i] - dp_table[i + x][m.max(x)]);
}
}
}
}
return dp_table[0][1];
}
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
assert_eq!(10, Solution::stone_game_ii(vec![2,7,9,4,4]));
}
#[test]
fn test_2() {
assert_eq!(104, Solution::stone_game_ii(vec![1,2,3,4,5,100]));
}
}