Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/546.html

546. Remove Boxes (Hard)

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: boxes = [1,3,2,2,2,3,4,3,1]
Output: 23
Explanation:
[1, 3, 2, 2, 2, 3, 4, 3, 1] 
---
##### Welcome to Subscribe On Youtube


-> [1, 3, 3, 4, 3, 1] (3*3=9 points) --- ##### Welcome to Subscribe On Youtube
-> [1, 3, 3, 3, 1] (1*1=1 points) --- ##### Welcome to Subscribe On Youtube
-> [1, 1] (3*3=9 points) --- ##### Welcome to Subscribe On Youtube
-> [] (2*2=4 points)

 

Constraints:

  • 1 <= boxes.length <= 100
  • 1 <= boxes[i] <= 100

Related Topics:
Dynamic Programming, Depth-first Search

Similar Questions:

Solution 1.

  • class Solution {
        public int removeBoxes(int[] boxes) {
            if (boxes == null || boxes.length == 0)
                return 0;
            int length = boxes.length;
            int[][][] dp = new int[length][length][length];
            return getPoints(boxes, dp, 0, length - 1, 0);
        }
    
        public int getPoints(int[] boxes, int[][][] dp, int low, int high, int count) {
            if (low > high)
                return 0;
            if (dp[low][high][count] != 0)
                return dp[low][high][count];
            while (low < high && boxes[high] == boxes[high - 1]) {
                high--;
                count++;
            }
            dp[low][high][count] = getPoints(boxes, dp, low, high - 1, 0) + (count + 1) * (count + 1);
            for (int i = low; i < high; i++) {
                if (boxes[i] == boxes[high])
                    dp[low][high][count] = Math.max(dp[low][high][count], getPoints(boxes, dp, low, i, count + 1) + getPoints(boxes, dp, i + 1, high - 1, 0));
            }
            return dp[low][high][count];
        }
    }
    
    ############
    
    class Solution {
        private int[][][] f;
        private int[] b;
    
        public int removeBoxes(int[] boxes) {
            b = boxes;
            int n = b.length;
            f = new int[n][n][n];
            return dfs(0, n - 1, 0);
        }
    
        private int dfs(int i, int j, int k) {
            if (i > j) {
                return 0;
            }
            while (i < j && b[j] == b[j - 1]) {
                --j;
                ++k;
            }
            if (f[i][j][k] > 0) {
                return f[i][j][k];
            }
            int ans = dfs(i, j - 1, 0) + (k + 1) * (k + 1);
            for (int h = i; h < j; ++h) {
                if (b[h] == b[j]) {
                    ans = Math.max(ans, dfs(h + 1, j - 1, 0) + dfs(i, h, k + 1));
                }
            }
            f[i][j][k] = ans;
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/remove-boxes/
    // Time: O(N^4)
    // Space: O(N^3)
    // Ref: https://leetcode.com/problems/remove-boxes/discuss/101310/Java-top-down-and-bottom-up-DP-solutions
    class Solution {
        vector<vector<vector<int>>> dp;
        int solve(vector<int> &A, int i, int j, int k) {
            if (i > j) return 0;
            if (dp[i][j][k] > 0) return dp[i][j][k];
            for (; i + 1 <= j && A[i + 1] == A[i]; ++i, ++k);
            int ans = (k + 1) * (k + 1) + solve(A, i + 1, j, 0);
            for (int m = i + 1; m <= j; ++m) {
                if (A[i] != A[m]) continue;
                ans = max(ans, solve(A, i + 1, m - 1, 0) + solve(A, m, j, k + 1));
            }
            return dp[i][j][k] = ans;
        }
    public:
        int removeBoxes(vector<int>& A) {
            int N = A.size();
            dp.assign(N, vector<vector<int>>(N, vector<int>(N)));
            return solve(A, 0, N - 1, 0);
        }
    };
    
  • class Solution:
        def removeBoxes(self, boxes: List[int]) -> int:
            @cache
            def dfs(i, j, k):
                if i > j:
                    return 0
                while i < j and boxes[j] == boxes[j - 1]:
                    j, k = j - 1, k + 1
                ans = dfs(i, j - 1, 0) + (k + 1) * (k + 1)
                for h in range(i, j):
                    if boxes[h] == boxes[j]:
                        ans = max(ans, dfs(h + 1, j - 1, 0) + dfs(i, h, k + 1))
                return ans
    
            n = len(boxes)
            ans = dfs(0, n - 1, 0)
            dfs.cache_clear()
            return ans
    
    ############
    
    class Solution(object):
      def removeBoxes(self, A):
        N = len(A)
        memo = [[[0] * N for _ in range(N)] for _ in range(N)]
    
        def dp(i, j, k):
          if i > j: return 0
          if not memo[i][j][k]:
            m = i
            while m + 1 <= j and A[m + 1] == A[i]:
              m += 1
            i, k = m, k + m - i
            ans = dp(i + 1, j, 0) + (k + 1) ** 2
            for m in range(i + 1, j + 1):
              if A[i] == A[m]:
                ans = max(ans, dp(i + 1, m - 1, 0) + dp(m, j, k + 1))
            memo[i][j][k] = ans
          return memo[i][j][k]
    
        return dp(0, N - 1, 0)
    
    
  • func removeBoxes(boxes []int) int {
    	n := len(boxes)
    	f := make([][][]int, n)
    	for i := range f {
    		f[i] = make([][]int, n)
    		for j := range f[i] {
    			f[i][j] = make([]int, n)
    		}
    	}
    	var dfs func(i, j, k int) int
    	dfs = func(i, j, k int) int {
    		if i > j {
    			return 0
    		}
    		for i < j && boxes[j] == boxes[j-1] {
    			j, k = j-1, k+1
    		}
    		if f[i][j][k] > 0 {
    			return f[i][j][k]
    		}
    		ans := dfs(i, j-1, 0) + (k+1)*(k+1)
    		for h := i; h < j; h++ {
    			if boxes[h] == boxes[j] {
    				ans = max(ans, dfs(h+1, j-1, 0)+dfs(i, h, k+1))
    			}
    		}
    		f[i][j][k] = ans
    		return ans
    	}
    	return dfs(0, n-1, 0)
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    

All Problems

All Solutions