-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0312_burst_balloons.rs
More file actions
46 lines (37 loc) · 1.12 KB
/
s0312_burst_balloons.rs
File metadata and controls
46 lines (37 loc) · 1.12 KB
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
#![allow(unused)]
pub struct Solution {}
use std::cmp::max;
impl Solution {
pub fn max_coins(mut nums: Vec<i32>) -> i32 {
let n = nums.len() + 2;
let mut new_nums = vec![0; n];
for i in 0..nums.len() {
new_nums[i + 1] = nums[i];
}
new_nums[n - 1] = 1;
new_nums[0] = 1;
// dp will store the results of our calls
let mut dp = vec![vec![0; n]; n];
// iterate over dp and incrementally build up to dp[0][n-1]
for left in (0..(n - 1)).rev() {
for right in (left + 2)..n {
for i in (left + 1)..right {
// same formula to get the best score from (left, right) as before
dp[left][right] = max(
dp[left][right],
new_nums[left] * new_nums[i] * new_nums[right] + dp[left][i] + dp[i][right],
);
}
}
}
dp[0][n - 1]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_312() {
assert_eq!(Solution::max_coins(vec![3, 1, 5, 8]), 167);
}
}