zoukankan      html  css  js  c++  java
  • LeetCode 546. Remove Boxes

    原题链接在这里:https://leetcode.com/problems/remove-boxes/

    题目:

    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:

    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)

    题解:

    Let T(l, r, k) denotes maximum points obtained within boxes[l~r], and there are k boxes on the right side of boxes[r] having same color as boxes[r].

    There could be 2 cases:

    case 1: remove boxes[r] with another k on the right of it and get (k+1)*(k+1) points. The rest is T(l, r-1, 0).

    case 2: Not remove boxes[r] for now. And for each i between l~r-1, if boxes[i] has same color as boxes[r]. divide it into 2 parts. T(l, i, k+1), r is accumlated to k+1. And T(i+1, r-1, 0).

    Some optimization is to to move r to left and increase k if boxes[r] and boxes[r-1] have same color. Save time to calculate remove boxes[r] because it must obintain more points remove boxes[r] and boxes[r-1] together.

    Time Complexity: O(n^4). n = boxes.length. dfs takes O(n). memo is 3 dimensional, thus it takes O(n^4) totally.

    Space: O(n^3).

    AC Java:

     1 class Solution {
     2     public int removeBoxes(int[] boxes) {
     3         int n = boxes.length;
     4         int [][][] dp = new int[n][n][n];
     5         return removeBoxesSub(boxes, 0, n-1, 0, dp);
     6     }
     7     
     8     private int removeBoxesSub(int [] boxes, int l, int r, int k, int [][][] dp){
     9         if(l>r){
    10             return 0;
    11         }
    12         
    13         while(l<r && boxes[r]==boxes[r-1]){
    14             r--;
    15             k++;
    16         }
    17         
    18         if(dp[l][r][k] > 0){
    19             return dp[l][r][k];
    20         }
    21         
    22         int res = removeBoxesSub(boxes, l, r-1, 0, dp) + (k+1)*(k+1);
    23         for(int i = l; i<r; i++){
    24             if(boxes[i] == boxes[r]){
    25                 res = Math.max(res, removeBoxesSub(boxes, l, i, k+1, dp)+removeBoxesSub(boxes, i+1, r-1, 0, dp));
    26             }
    27         }
    28         
    29         dp[l][r][k] = res;
    30         return res;
    31     }
    32 }

    类似Burst Balloons.

  • 相关阅读:
    SCSI contrller的几种类型的区别
    ScaleIO与XtremSW Cache如何集成呢?
    如何强制使用某一大小的包去ping某个IP地址?
    如何查看ETW Trace?
    图像卷积与滤波的一些知识点(转)
    tensorflow serving 编写配置文件platform_config_file的方法
    python在linux的报错集锦
    某些数组和字符串类型转换(转)
    系统安装-007 CentOS7yum源添加、删除及其yum优化(转)
    Error:Failed to resolve: android.arch.core:common:1.1.0
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11660827.html
Copyright © 2011-2022 走看看