题目链接:https://leetcode.com/problems/remove-boxes/description/
Given several boxes with different colors represented by different positive numbers.
You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes, k >= 1), remove them and get k*k
points.
Find the maximum points you can get.
Example 1:
Input:
[1, 3, 2, 2, 2, 3, 4, 3, 1]Output:
23Explanation:
[1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3, 1] (3*3=9 points) ----> [1, 3, 3, 3, 1] (1*1=1 points) ----> [1, 1] (3*3=9 points) ----> [] (2*2=4 points)
Note: The number of boxes n
would not exceed 100.
When facing this problem, I am keeping thinking how to simulate the case when boxes[i] == boxes[j]
when i
and j
are not consecutive. It turns out that the dp matrix needs one more dimension to store such state. So we are going to define the state as
dp[i][j][k] represents the max points from box[i] to box[j] with k boxes whose values equal to box[i]
The transformation function is as below
dp[i][j][k] = max(dp[i+1][m-1][1] + dp[m][j][k+1]) when box[i] = box[m]
Here, we partition the array into two: [b_i,b_m,..., b_j]
, and [b_{i+1}, ..., b_{m-1}]
. The solution for first one will be memo[m][j][k+1]
, and the second will be memo[i+1][m-1][0]
.
class Solution {
public:
int removeBoxes(vector& boxes) {
if(boxes.empty())
return 0;
int size=boxes.size();
int memo[100][100][100]={0};
return dfs(boxes,memo,0,size-1,1);
}
int dfs(vector boxes,int &memo[100][100][100],int i,int j,int k)
{
if(i>j)
return 0;
else if(i==j)
return k*k;
else if(memo[i][j][k]!=0)
return memo[i][j][k];
else
{
int temp=dfs(boxes,memo,i+1,j,1)+k*k;
for(int m=i+1;m<=j;m++)
{
if(boxes[i]==boxes[m])
{
temp=max(temp,dfs(boxes,memo,i+1,m-1,1)+dfs(boxes,memo,m,j,k+1));
}
}
memo[i][j][k]=temp;
return temp;
}
}
};
I think the problem of the approach above is that there are a lot of unnecessary computations (not recomputations). For example, if there is a formation of ABCDAA
, we know the optimal way is B->C->D->AAA
. On the other hand, if the formation is BCDAA
, meaning that we couldn't find an A
before D
, we will simply remove AA
, which will be the optimal solution for removing them. Note this is true only if AA
is at the end of the array. With naive memoization approach, the program will search a lot of unnecessary paths, such as C->B->D->AA
, D->B->C->AA
.
Therefore, I designed the memoization matrix to be memo[l][r][k]
, the largest number we can get using l
th to r
th (inclusive) boxes with k same colored boxes as r
th box appended at the end. Example, memo[l][r][3]
represents the solution for this setting: [b_l, ..., b_r, A,A,A]
with b_r==A
.
The transition function is to find the maximum among all b_i==b_r
for i=l,...,r-1
:
memo[l][r][k] = max(memo[l][r][k], memo[l][i][k+1] + memo[i+1][r-1][0])
Basically, if there is one i
such that b_i==b_r
, we partition the array into two: [b_l, ..., b_i, b_r, A, ..., A]
, and [b_{i+1}, ..., b_{r-1}]
. The solution for first one will be memo[l][i][k+1]
, and the second will be memo[i+1][r-1][0]
. Otherwise, we just remove the last k+1 boxes (including b_r
) and search the best solution for l
th to r-1
th boxes. (One optimization here: make r
as left as possible, this improved the running time from 250ms to 35ms)
The final solution is stored in memo[0][n-1][0]
for sure.
I didn't think about this question for a long time in the contest because the time is up. There will be a lot of room for time and space optimization as well. Thus, if you find any flaws or any improvements, please correct me.
class Solution {
public:
int removeBoxes(vector& boxes) {
int n=boxes.size();
int memo[100][100][100] = {0};
return dfs(boxes,memo,0,n-1,0);
}
int dfs(vector& boxes,int memo[100][100][100], int l,int r,int k){
if (l>r) return 0;
if (memo[l][r][k]!=0) return memo[l][r][k];
while (r>l && boxes[r]==boxes[r-1]) {r--;k++;}
memo[l][r][k] = dfs(boxes,memo,l,r-1,0) + (k+1)*(k+1);
for (int i=l; i