Remove Boxes – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
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 <= 100
1 <= boxes[i] <= 100
C++ Remove Boxes LeetCode Solution
class Solution {
public:
int memo[200][200][200] = {};
int removeBoxes(vector<int>& boxes) {
return dp(boxes, 0, boxes.size() – 1, 0);
}
int dp(vector<int>& boxes, int l, int r, int k) {
if (l > r) return 0;
if (memo[l][r][k] > 0) return memo[l][r][k];
int lOrg = l, kOrg = k;
while (l+1 <= r && boxes[l] == boxes[l+1]) { // Increase both `l` and `k` if they have consecutive colors with `boxes[l]`
l += 1;
k += 1;
}
int ans = (k+1) * (k+1) + dp(boxes, l+1, r, 0); // Remove all boxes which has the same with `boxes[l]`
for (int m = l + 1; m <= r; ++m) // Try to merge non-contiguous boxes of the same color together
if (boxes[m] == boxes[l])
ans = max(ans, dp(boxes, m, r, k+1) + dp(boxes, l+1, m-1, 0));
return memo[lOrg][r][kOrg] = ans;
}
};
Java Remove Boxes LeetCode Solution
class Solution {
int[][][] memo;
public int removeBoxes(int[] boxes) {
int n = boxes.length;
memo = new int[n][n][n];
return dp(boxes, 0, n – 1, 0);
}
int dp(int[] boxes, int l, int r, int k) {
if (l > r) return 0;
if (memo[l][r][k] > 0) return memo[l][r][k];
int lOrg = l, kOrg = k;
while (l+1 <= r && boxes[l] == boxes[l+1]) { // Increase both `l` and `k` if they have consecutive colors with `boxes[l]`
l += 1;
k += 1;
}
int ans = (k+1) * (k+1) + dp(boxes, l+1, r, 0); // Remove all boxes which has the same with `boxes[l]`
for (int m = l+1; m <= r; ++m) // Try to merge non-contiguous boxes of the same color together
if (boxes[m] == boxes[l])
ans = Math.max(ans, dp(boxes, m, r, k+1) + dp(boxes, l+1, m-1, 0));
return memo[lOrg][r][kOrg] = ans;
}
}
Python 3 Remove Boxes LeetCode Solution
class Solution {
int[][][] memo;
public int removeBoxes(int[] boxes) {
int n = boxes.length;
memo = new int[n][n][n];
return dp(boxes, 0, n – 1, 0);
}
int dp(int[] boxes, int l, int r, int k) {
if (l > r) return 0;
if (memo[l][r][k] > 0) return memo[l][r][k];
int lOrg = l, kOrg = k;
while (l+1 <= r && boxes[l] == boxes[l+1]) { // Increase both `l` and `k` if they have consecutive colors with `boxes[l]`
l += 1;
k += 1;
}
int ans = (k+1) * (k+1) + dp(boxes, l+1, r, 0); // Remove all boxes which has the same with `boxes[l]`
for (int m = l+1; m <= r; ++m) // Try to merge non-contiguous boxes of the same color together
if (boxes[m] == boxes[l])
ans = Math.max(ans, dp(boxes, m, r, k+1) + dp(boxes, l+1, m-1, 0));
return memo[lOrg][r][kOrg] = ans;
}
}
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1