leetcode 664. Strange Printer 546. Remove Boxes

本文详细解析了LeetCode上的两个题目:664.StrangePrinter 和 546.RemoveBoxes 的算法解决方案。通过自底向上和自顶向下的动态规划方法,阐述了如何有效地解决这两个涉及字符串操作和盒子移除的问题。

There is a strange printer with the following two special requirements:

  1. The printer can only print a sequence of the same character each time.
  2. At each turn, the printer can print new characters starting from and ending at any places, and will cover the original existing characters.

 

Given a string consists of lower English letters only, your job is to count the minimum number of turns the printer needed in order to print it.

Example 1:

Input: "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".

 

Example 2:

Input: "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.

 

Hint: Length of the given string will not exceed 100.


664. Strange Printer 比 546. Remove Boxes简单,后者是前者的升级版。所以先写 664. Strange Printer。

这种题,先在脑中模拟如何工作的,选择一种有规律可循的策略,可以想如何分解为子问题。

可以从最左边开始涂,第一次把字符串都涂满(贪心思想),因为如果左边如果不是第一个涂,也可交换次序。以左边涂满后与最后完成时右边第一个保留的相同颜色的点,把问题分成子问题,右边第一个保留的点看作一个起始点。

对于每个s[i]==s[k],dp[i][j]=Math.max(1+dp[i+1][j],dp[i+1][k-1]+dp[k][j]);

时间复杂度O(n^3),空间复杂度O(n^2).

class Solution {
    public int strangePrinter(String s) {
        int len=s.length();
        if(len==0) return 0;
        int[][] dp=new int[len][len];
        for(int k=0;k<len;k++){
            for(int i=0;i+k<len;i++){
                dp[i][i+k]=k+1;
                for(int j=i;j<=i+k;j++){
                    if(s.charAt(i)==s.charAt(j)){
                        if(i==j){
                            dp[i][i+k]=1+(k==0?0:dp[j+1][i+k]);
                        }else{
                            dp[i][i+k]=Math.min(dp[i][i+k],(i+1>j-1?0:dp[i+1][j-1])+dp[j][i+k]);    
                        }
                    }
                }       
            }
        }
        return dp[0][len-1];
    }
}

=============================================================================

 

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)

 

Note: The number of boxes n would not exceed 100.


 

这题和上一题划分子问题的方式还是一样的,但比上一题多了一个分数的限制,需要在增加一维表式左边含有当前boxes[l]的个数。感觉这题自底向上写dp比较麻烦,参考了讨论区大神的做法是自顶向下方式写的dp比较多。所以这里也可以看出:有时记忆化dfs与dp的本质是一样的。自底向上dp待写。

另外需要适当的剪枝,初始连续的箱子就不会分开移动。

时间复杂度O(n^4),空间复杂度O(n^3)。

class Solution {
    int[][][] dp=new int[100][100][100];
    public int removeBoxes(int[] boxes) {
        return dfs(boxes,0,boxes.length-1,0);
    }
    int dfs(int[] boxes,int l,int r,int c){
        if(dp[l][r][c]>0) return dp[l][r][c];
        if((l<r&&boxes[l]!=boxes[l+1])||(l==r)) dp[l][r][c]=(1+c)*(1+c)+(l+1>r?0:dfs(boxes,l+1,r,0));
        for(int i=l+1;i<=r;i++){
            if(boxes[i]==boxes[l]){
                int p=i;
                int c1=c;
                while(i<=r&&boxes[i]==boxes[l]){//剪枝
                    c1++;
                    i++;
                }
                dp[l][r][c]=Math.max(dp[l][r][c],(l+1>p-1?0:dfs(boxes,l+1,p-1,0))+dfs(boxes,i-1,r,c1));
            }
        }
        return dp[l][r][c];
    }
}

参考:https://leetcode.com/problems/remove-boxes/discuss/101310/Java-top-down-and-bottom-up-DP-solutions

### 如何在 VSCode 中安装和配置 LeetCode 插件以及 Node.js 运行环境 #### 安装 LeetCode 插件 在 VSCode 的扩展市场中搜索 `leetcode`,找到官方提供的插件并点击 **Install** 按钮进行安装[^1]。如果已经安装过该插件,则无需重复操作。 #### 下载与安装 Node.js 由于 LeetCode 插件依赖于 Node.js 环境,因此需要下载并安装 Node.js。访问官方网站 https://nodejs.org/en/ 并选择适合当前系统的版本(推荐使用 LTS 版本)。按照向导完成安装流程后,需确认 Node.js 是否成功安装到系统环境中[^2]。 可以通过命令行运行以下代码来验证: ```bash node -v npm -v ``` 上述命令应返回对应的 Node.js 和 npm 的版本号。如果没有正常返回版本信息,则可能未正确配置环境变量。 #### 解决环境路径问题 即使完成了 Node.js 的安装,仍可能出现类似 “LeetCode extension needs Node.js installed in environment path” 或者 “command ‘leetcode.toggleLeetCodeCn’ not found” 的错误提示[^3]。这通常是因为 VSCode 未能识别全局的 Node.js 路径或者本地安装的 nvm 默认版本未被正确加载[^4]。 解决方法如下: 1. 手动指定 Node.js 可执行文件的位置 在 VSCode 设置界面中输入关键词 `leetcode`,定位至选项 **Node Path**,将其值设为实际的 Node.js 安装目录下的 `node.exe` 文件位置。例如:`C:\Program Files\nodejs\node.exe`。 2. 使用 NVM 用户管理工具调整默认版本 如果通过 nvm 工具切换了不同的 Node.js 版本,请确保设置了默认使用的版本号。可通过以下指令实现: ```bash nvm alias default <version> ``` 重新启动 VSCode 后测试功能键是否恢复正常工作状态。 --- #### 配置常用刷题语言 最后一步是在 VSCode 设置面板中的 LeetCode 插件部分定义个人习惯采用的主要编程语言作为默认提交方式之一。这样可以减少频繁修改编码风格的时间成本。 --- ### 总结 综上所述,要在 VSCode 上顺利启用 LeetCode 插件及其关联服务,除了基本插件本身外还需额外准备支持性的后台框架——即 Node.js 应用程序引擎;同时针对特定场景下产生的兼容性障碍采取针对性措施加以修正即可达成目标[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值