// 加入两个虚拟气球 points := make([]int, n + 2) points[0], points[n + 1] = 1, 1 for i := 1; i <= n; i++ { points[i] = nums[i-1] }
dp := make([][]int, n + 2) for i := range dp { dp[i] = make([]int, n + 2) }
// i 和 j 的遍历顺序根据 DP 表图示 for i := n; i >= 0; i-- { for j := i + 1; j <= n + 1; j++ { // 最后戳破哪个气球,都尝试一遍 for k := i + 1; k < j; k++ { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + points[i]*points[k]*points[j]) } } }
return dp[0][n+1] }
funcmax(values ...int)int { res := values[0] for _, v := range values { if v > res { res = v } } return res }