Leetcode: 664. Strange Printer
来源:互联网 发布:装饰行业网络销售 编辑:程序博客网 时间:2024/05/29 17:57
This problem is simiiar to #546(Leetcode) 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
#include <string>#include <cmath>class Solution{private: int f[100][100];private: int dfs(const std::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] = std::min(f[l][r], dfs(s, l, i) + dfs(s, i + 1, r - 1)); } } return f[l][r]; }public: int strangePrinter(std::string s) { memset(f, 0, sizeof(f)); int len = (int)s.size(); return dfs(s, 0, len - 1); }};
阅读全文
0 0
- [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
- Android四大组件启动分析
- PostgreSQL(七)备份还原
- 闲暇之余
- 一.09模块
- 用Construct2制作游戏大致过程
- Leetcode: 664. Strange Printer
- linux是什么
- 高质量API网关借口设计教程
- HTML5&CSS3(2)
- noip 并查集
- D
- 事务的ACID特性
- Python机器学习算法 推荐
- Android二维码扫描模块可简单集成(基于ZXIng实现)