You are 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 (i.e., composed of k boxes, k >= 1), remove them and get k * k points.
Return the maximum points you can get.
Example 1:
Input: boxes = [1,3,2,2,2,3,4,3,1] Output: 23 Explanation: [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)
Example 2:
Input: boxes = [1,1,1] Output: 9
Example 3:
Input: boxes = [1] Output: 1
Constraints:
1 <= boxes.length <= 1001 <= boxes[i] <= 100This approach uses recursion and memoization to compute the maximum score recursively. We artificially modify the problem to keep count of contiguous boxes and use a 3D array for memoization. The base case is when there are no boxes left. Recurrence relations deal with choosing to remove a contiguous segment immediately or merge it with other parts of the array by considering different partition points.
The recursive dp function tries to match different segments of the array and calculates scores accordingly. dp(l, r, k) computes the maximum score for a subarray while considering preprocessing contiguous blocks at index r. By reducing r to a point where we have a different color or run out of elements to compare, we explore subproblems and store results in the memoization table to save on future calculations.
JavaScript
Time Complexity: O(n^4). The recursive solution has three dimensions for recursion state, each from 0 to n.
Space Complexity: O(n^3). Needed for memoization storage.
This approach converts the recursive problem into an iterative dynamic programming solution. We maintain a 3D table to determine maximum obtainable points for different contiguous subarrays and contingent blocks for each color. We fill the table starting from smaller segments and incrementally consider larger segments while determining scores.
The solution populates a 3D dp table by fixing lengths and starting index l of segments before solving larger segments. For each possible partition index m where blocks match color l of the segments, scores are merged and computed effectively. The iterative solution ensures no recursion overhead and efficiently computes all subproblem solutions iteratively.
Time Complexity: O(n^4). Similar state iterations but performed iteratively.
Space Complexity: O(n^3). Space used for the iterative 3D table.
| Approach | Complexity |
|---|---|
| Recursive Dynamic Programming with Memoization | Time Complexity: O(n^4). The recursive solution has three dimensions for recursion state, each from 0 to n. |
| Iterative Dynamic Programming | Time Complexity: O(n^4). Similar state iterations but performed iteratively. |
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD • Striver • 522,243 views views
Watch 9 more video solutions →Practice Remove Boxes with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor