[leetcode]664. Strange Printer
来源:互联网 发布:mac foxmail 邮件备份 编辑:程序博客网 时间:2024/05/16 04:18
题目链接:https://leetcode.com/problems/strange-printer/description/
There is a strange printer with the following two special requirements:
- The printer can only print a sequence of the same character each time.
- 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: 2Explanation: Print "aaa" first and then print "bbb".
Example 2:
Input: "aba"Output: 2Explanation: 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.
This problem is simiiar to #546 Remove Boxes which uses f[l][r][k]
to store the maximum points of range [l, r]
with k
boxes equal to r
. But for this problem, we can use 2D-array DP instead of 3D-array DP because the store of k
is useless.
f[i][j]
represents the minumum turns to print the sequence from i
to j
. The transition function should be:
f[i][j] = min(f[i][k] + f[k+1][j-1]) for each k where i<k<j and s[k]=s[j]
Do not forget the common transition:
f[i][j] = f[i][j-1] + 1
And the border condition:
f[i][j] = 0 where i>j
class Solution {public: int strangePrinter(string s) { memset(f,0, sizeof(f)); int size=s.size(); return dfs(s,0,size-1); }private: int f[100][100]; int dfs(string &s,int l,int r) { if(l>r) return 0; if(f[l][r]) return f[l][r]; f[l][r]=dfs(s,l,r-1)+1; for(int i=l;i<r;i++) { if(s[i]==s[r]) { f[l][r]=min(f[l][r],dfs(s,l,i)+dfs(s,i+1,r-1)); } } return f[l][r]; }};
- [leetcode]664. Strange Printer
- [Leetcode]664. Strange Printer
- Leetcode: 664. Strange Printer
- Strange Printer (leetcode)
- 664. Strange Printer
- 664. Strange Printer
- 【LeetCode】664.Strange Printer(hard)解题报告
- leetcode 664. Strange Printer 奇怪的打印机 + 动态规划DP
- LeetCode: -Dynamic Programming-Strange Printer[664]
- Strange Printer
- 664Strange Printer
- leetcode_664. Strange Printer ? 待解决
- 算法练习(7) —— 动态规划 Strange Printer
- Printer
- Printer Printer
- Printer.cs
- printer repair
- linux printer
- 添加Nginx作为系统服务
- 查看tensorflow ckpt文件中的变量名和对应值
- 【009】Java语言(环境变量配置-临时配置方法)
- phpcms v9.6.0版本getshell测试
- 快捷键大全-私人收藏
- [leetcode]664. Strange Printer
- 常见排序算法(时,空)复杂度总结及稳定性分析
- CentOS7.3安装部署wordpress
- SGISTL源码探究-vector容器(上)
- 98. Validate Binary Search Tree
- [单调队列] POJ2823
- ICPC2017网络赛(乌鲁木齐)E: Half-consecutive Numbers (大数)
- vyos
- Java NIO 介绍