Leetcode 664.奇怪的打印机
奇怪的打印机
有台奇怪的打印机有以下两个特殊要求:
- 打印机每次只能打印同一个字符序列。
- 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符。
给定一个只包含小写英文字母的字符串,你的任务是计算这个打印机打印它需要的最少次数。
示例 1:
输入: "aaabbb"
输出: 2
解释: 首先打印 "aaa" 然后打印 "bbb"。
示例 2:
输入: "aba"
输出: 2
解释: 首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
提示: 输入字符串的长度不会超过 100。
class Solution {
int[][] memo;
public int strangePrinter(String s) {
int N = s.length();
memo = new int[N][N];
return dp(s, 0, N - 1);
}
public int dp(String s, int i, int j) {
if (i > j) return 0;
if (memo[i][j] == 0) {
int ans = dp(s, i+1, j) + 1;
for (int k = i+1; k <= j; ++k)
if (s.charAt(k) == s.charAt(i))
ans = Math.min(ans, dp(s, i, k-1) + dp(s, k+1, j));
memo[i][j] = ans;
}
return memo[i][j];
}
}
Leetcode 664.奇怪的打印机的更多相关文章
- Java实现 LeetCode 664 奇怪的打印机(DFS)
664. 奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符 ...
- [LeetCode] Strange Printer 奇怪的打印机
There is a strange printer with the following two special requirements: The printer can only print a ...
- [Swift]LeetCode664. 奇怪的打印机 | Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)
题目: There is a strange printer with the following two special requirements: The printer can only pri ...
- leetcode 664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- LeetCode刷题总结-动态规划篇
本文总结LeetCode上有动态规划的算法题,推荐刷题总数为54道.具体考点分析如下图: 1.中心扩展法 题号:132. 分割回文串 II,难度困难 2.背包问题 题号:140. 单词拆分 II,难度 ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
随机推荐
- lambda函数,内置map()函数及filter()函数
8.1 lambda函数 作用及意义: 1.没必要专门定义函数,给函数起名,起到精简的效果 2.简化代码的可读性 def ds(x): return 2 * x + 1 ds(5) ---11 g ...
- MySQL常见错误分析与解决方法总结
MySQL常见错误分析与解决方法总结 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分 ...
- JS中的执行环境和作用域
window 是最大最外围的执行环境,然后每个函数都有自己的执行环境.JS代码是从上到下执行的,单纯的用语言描述可能会有点绕,而且不大直观.我们看着代码来 console.log('global be ...
- NPM 学习笔记整理
NPM 学习笔记整理 阅读 550,2017年06月04日 发布,来源:blog.ihoey.com 什么是 NPM npm 之于 Node ,就像 pip 之于 Python , gem 之于 Ru ...
- floyed dij spfa 模板
/* SPFA模板 */ const int inf=0x3f3f3f3f; inline int SPFA(int s){ memset(dis,inf,sizeof(dis)); queue< ...
- UOJ#179. 线性规划(线性规划)
描述 提交 自定义测试 这是一道模板题. (这个题现在标程挂了..哪位哥哥愿意提供一下靠谱的标程呀?) 本题中你需要求解一个标准型线性规划: 有 nn 个实数变量 x1,x2,…,xnx1,x2,…, ...
- Win10上Anaconda环境下python3.6安装和使用pyinstaller
一.安装步骤 1. 电脑是win10,安装的Python3.6 2. 在Scripts文件夹下执行pip install pyinstaller, 安装成功后下载pyinstaller安装包,解压之后 ...
- React路由-基础篇
React-Router-DOM ReactRouter网址, 安装 -npmjs找到react-router-dom -yarn add react-router-dom 基本使用方法 1.创建一个 ...
- Fiddler(二)
该博客基于以下博客网站里的内容进行提取,实验,和补充.让我们开始 https://www.cnblogs.com/yyhh/p/5140852.html AutoResponder 允许拦截指定规则的 ...
- python__高级 : 类的__getattribute__ 方法
在类 里面,其实并没有方法这个东西,所有的东西都保存在属性里面,所谓的调用方法其实是类里面的一个同名属性指向了一个函数(方法),返回的是函数的引用,再用 函数() 这种方式就可以调用它 在调 ...