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 ...
随机推荐
- 2017.10.15 解析Java中抽象类和接口的区别
(1)在Java语言中,abstract class 和interface 是支持抽象类定义的两种机制. 正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和 ...
- Docker 入门教程与实践
title: Docker 入门教程与实践 tags: Docker ---- 在Windows上安装Docker客户端 1.下载Docker TollBox: https://docs.docker ...
- Tomcat8.0.36安装配置
1.下载tomcat8.0.36 下载地址:http://tomcat.apache.org/download-80.cgi 2.解压 至C:\Program Files\tomcat8下 3.添加系 ...
- 浅谈Docker
一.为什么使用Docker 软件开发最大的麻烦事之一,就是环境配置.很多人想到,能不能从根本上解决问题,软件可以带环境安装? 也就是说,安装的时候,把原始环境一模一样地复制过来. 目前有两个主流解决方 ...
- esdoc 自动生成接口文档介绍
原文地址:https://www.xingkongbj.com/blog/esdoc/creat-esdoc.html 官网 ESDoc:https://esdoc.org/ JSDoc:http:/ ...
- 灵光一现的trick
感觉平时会丢掉好多挺好的trick…… 图论 1.图G,固定S,T.可以将任意一条边加上权值$k(k>0)$,求最大化加权后最短路. 2.图G,固定S,T.可以将任意一条边乘以权值$k(k> ...
- 【JavaScript】jQuery绑定事件
jquery中直接绑定事件:只能用在程序中一开始就存在的html代码 目标元素.click(function(){ }) jquery中间接绑定事件: 如果目标元素是js生成的,则需要间接绑定事件,用 ...
- SQL tp3.2 批量更新 saveAll
/** * 批量更新数据 * @param [array] $datas [更新数据] * @param [string] $table_name [表名] */ public function sa ...
- python3 练习题100例 (二十)
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""练习二十:判断一个年份是否是闰年公历闰年计算方法:1.普通年能被4整除且不 ...
- No module named 'PyQt5.sip'
使用pyinstaller打包python文件为windows可执行程序可能遇到的问题 pyinstaller yourprogram.py打包的程序双击打开一闪而过,提示上面标题的错误 把pycom ...