题目:
题解:
Go
func removeBoxes(boxes []int) int {
dp := [100][100][100]int{}
var calculatePoints func(boxes []int, l, r, k int) int
calculatePoints = func(boxes []int, l, r, k int) int {
if l > r {
return 0
}
if dp[l][r][k] == 0 {
r1, k1 := r, k
for r1 > l && boxes[r1] == boxes[r1 - 1] {
r1--
k1++
}
dp[l][r][k] = calculatePoints(boxes, l, r1 - 1, 0) + (k1 + 1) * (k1 + 1)
for i := l; i < r1; i++ {
if boxes[i] == boxes[r1] {
dp[l][r][k] = max(dp[l][r][k], calculatePoints(boxes, l, i, k1 + 1) + calculatePoints(boxes, i + 1, r1 - 1, 0))
}
}
}
return dp[l][r][k]
}
return calculatePoints(boxes, 0, len(boxes) - 1, 0)
}
func max(x, y int) int {
if x > y {
return x
}
return y
}