dp
with dimensions [n][n][n]
where n
is the length of the boxes
array.calculatePoints(l, r, k)
that returns the maximum points for the subarray boxes[l:r]
with k
additional elements similar to boxes[r]
following it.dp[l][r][k]
is already computed, return it to avoid redundant calculations.dp[l][r][k]
with the value obtained by removing the last k+1
similar elements.boxes[l:r]
to find elements similar to boxes[r]
.i
, calculate the potential points by combining it with the trailing similar elements and update dp[l][r][k]
if it leads to a higher score.dp[0][n-1][0]
as the final result, which represents the maximum points for the entire array with no additional similar elements at the end.