<p>You are given several <code>boxes</code> with different colors represented by different positive numbers.</p>

<p>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 (i.e., composed of <code>k</code> boxes, <code>k &gt;= 1</code>), remove them and get <code>k * k</code> points.</p>

<p>Return <em>the maximum points you can get</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> boxes = [1,3,2,2,2,3,4,3,1]
<strong>Output:</strong> 23
<strong>Explanation:</strong>
[1, 3, 2, 2, 2, 3, 4, 3, 1] 
----&gt; [1, 3, 3, 4, 3, 1] (3*3=9 points) 
----&gt; [1, 3, 3, 3, 1] (1*1=1 points) 
----&gt; [1, 1] (3*3=9 points) 
----&gt; [] (2*2=4 points)
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> boxes = [1,1,1]
<strong>Output:</strong> 9
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> boxes = [1]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= boxes.length &lt;= 100</code></li>
	<li><code>1 &lt;= boxes[i]&nbsp;&lt;= 100</code></li>
</ul>

class Solution:
    def removeBoxes(self, boxes: List[int]) -> int:
        # return self.max_points_memoize(boxes)
        return self.max_points_bottom_up(boxes)

    def max_points_memoize(self, boxes):
        # time: O(n^4) because helper has n^3 states and inside we can be looping upto n each call
        # space: O(n^3)
        
        n = len(boxes)
        dp = [[[-1 for _ in range(n)] for _ in range(n)] for _ in range(n)]

        def helper(i, j, k):
            # if i == j: # if at same box, we can only get point of 1, actuially no, we have to consider k
            # but actually better to let recursion handle this
            #     return (k + 1) * (k + 1)

            if i > j:
                return 0  # passed the boundary

            if dp[i][j][k] != -1:
                return dp[i][j][k]
            i0, k0 = i, k # save original function call values. We will use this to update cache dp

            # the total count we find of box[i]
            # remember k is the total we have BEFORE we get to box[i], so we never counted box[i]
            while i + 1 <= j and boxes[i] == boxes[i + 1]:
                # while we keep finding the same box, we can move i forward and increment count
                k += 1
                i += 1

            # Why DP check here works and not the usual place above???
            # Notice that we are changing the values of i and k, that we entered the function with
            # and before returning, if we save like the usual way, we assign dp[i][j][k] = max_points
            # BUT this i and k are the UPDATED values!!!
            # What we can infer is that, we are saving the result of the subproblem and i,j,k (updated) but checking for i,j,k (original) when entering the question
            # think about it for a second and it will make sense
            # So there are two ways to fix this
            # 1 -> We either perform the dp check here, so essentially we are reaching the same subproblem before making the check
            # 2 -> Save original values of i and k, and use that to store the subproblem result in the dp.
            # Second one makes more sense, because it goes with the line of flow. Save the subproblem result with what the function was called and check at the top
            # So we will use that, however making the check with updated values here works too!
            # if dp[i][j][k] != -1:
            #     return dp[i][j][k]

            # now here i will be pointing to the last box that equals boxes[i]
            # and k + 1 will be total count, since k was total count before current box[i]. And we increased k when we found a matching at i+1,
            # so basically we increased by 1 and had 2 same boxes, so need to account for that

            max_points = (k + 1) * (k + 1) + helper(i + 1, j, 0)  # this is the max points we can get with current box, and then we let recursion handle the rest
            # we reset count to 0 here, since we are starting from a new range with no match

            # now we also need to consider the case where if we remove the middle boxes first, can we find the same box[i] in the future?
            for partition in range(i + 1, j + 1):  # wanna go after current box, but go until j range

                if boxes[i] == boxes[
                    partition]:  # if we find that a box matches, we can consider that subproblem and pass the current count of boxes
                    # the recursion will handle the points of middle boxes
                    # f(partition, j, k+1) because we wanna start counting the score from partition because that equals the box[i]
                    # j because we wanna go until the current rang end
                    # k + 1 is the count of box[i] not including box[partition]. Remember, we undercount above
                    # and we also need to consider the subproblem of middle boxes that will be starting after i, and ending before partition with 0 count
                    max_points = max(max_points, helper(partition, j, k + 1) + helper(i + 1, partition - 1, 0))

            dp[i0][j][k0] = max_points
            return dp[i0][j][k0]

        return helper(0, n - 1, 0)

    def max_points_bottom_up(self, boxes):
        n = len(boxes)
        dp = [[[0 for _ in range(n)] for _ in range(n)] for _ in range(n)]

        for i in range(n - 1, -1, -1):
            for j in range(i, n):
                # k can go in this range, if our left range is i and k means the same boxes seen BEFORE index i
                # so we can count from 0,1,2,...i. So eg if i = 4, we count 0,1,2,3 = 4 count. This is before counting index 4
                # so basically k can go from 0 to i (inclusive)
                for k in range(i + 1):
                    # if i == j:
                    #     dp[i][j][k] = (k + 1) * (k + 1)
                    #     continue

                    # max_points = 0
                    # if i + 1 <= j:
                    max_points = (k + 1) * (k + 1) + dp[i + 1][j][0] if i + 1 <= j else (k + 1) * (k + 1)
                    # else:
                    #     max_points = (k + 1) * (k + 1)

                    for partition in range(i + 1, j + 1):
                        if boxes[i] == boxes[partition]:
                            # print(i, j, k+1)
                            max_points = max(max_points, dp[partition][j][k + 1] + dp[i + 1][partition - 1][0])

                    dp[i][j][k] = max_points

        # print(dp)
        return dp[0][n - 1][0]
30700