[Swift]LeetCode664. 奇怪的打印机 | Strange Printer
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10492196.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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: 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.
有台奇怪的打印机有以下两个特殊要求:
- 打印机每次只能打印同一个字符序列。
- 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符。
给定一个只包含小写英文字母的字符串,你的任务是计算这个打印机打印它需要的最少次数。
示例 1:
输入: "aaabbb" 输出: 2 解释: 首先打印 "aaa" 然后打印 "bbb"。
示例 2:
输入: "aba" 输出: 2 解释: 首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
提示: 输入字符串的长度不会超过 100。
116ms
1 class Solution { 2 func strangePrinter(_ s: String) -> Int { 3 var c = [Character]() 4 var pre : Character = " " 5 for (_, char) in s.enumerated() { 6 if char != pre { 7 c.append(char) 8 pre = char 9 } 10 } 11 var l = [[Int]](repeating:[Int](repeating: 0, count:c.count) , count:c.count) 12 return turn(c, 0, c.count - 1, &l) 13 } 14 15 func turn(_ c: [Character], _ i: Int, _ j: Int, _ l: inout [[Int]]) -> Int { 16 if i > j { 17 return 0 18 } 19 if l[i][j] > 0 { 20 return l[i][j] 21 } 22 var ans = turn(c, i, j-1, &l) + 1 23 for k in i ..< j { 24 if c[k] == c[j] { 25 ans = min(ans, turn(c, i, k, &l) + turn(c, k+1, j-1, &l)) 26 } 27 } 28 l[i][j] = ans 29 return ans 30 } 31 }
1 class Solution { 2 func strangePrinter(_ s: String) -> Int { 3 if s.isEmpty {return 0} 4 var arr:[Character] = Array(s) 5 var n:Int = s.count 6 var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n),count:n) 7 for i in (0...(n - 1)).reversed() 8 { 9 if(n < i) {continue} 10 for j in i..<n 11 { 12 dp[i][j] = (i == j) ? 1 : (1 + dp[i + 1][j]) 13 if(j < i + 1) {continue} 14 for k in (i + 1)...j 15 { 16 if arr[k] == arr[i] 17 { 18 dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k][j]) 19 } 20 } 21 } 22 } 23 return (n == 0) ? 0 : dp[0][n - 1] 24 } 25 }
转载于:https://www.cnblogs.com/strengthen/p/10492196.html
[Swift]LeetCode664. 奇怪的打印机 | Strange Printer相关推荐
- 算法练习(7) —— 动态规划 Strange Printer
算法练习(7) -- 动态规划 Strange Printer 动态规划 动态规划算法通常处理的是多阶段的决策最优化问题.挺多的问题都含有递推的思想.做这样的问题,最重要的就是找到对应的状态转移方程. ...
- 【冰爪编程】LintCode 解码大全 —— 1100 奇怪的打印机
[冰爪编程]LintCode 解码大全 -- 1100 奇怪的打印机 作者:BZIClaw 1100 奇怪的打印机 Python: class Solution:"""@ ...
- 【数据结构与算法】之深入解析“奇怪的打印机”的求解思路与算法示例
一.题目要求 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印由同一个字符组成的序列: 每次可以在从起始到结束的任意位置打印新字符,并且会覆盖掉原来已有的字符. 给你一个字符串 s,你的任务是 ...
- leetcode 664. 奇怪的打印机(dp)
题目 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印由 同一个字符 组成的序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给你一个字符串 s ,你的任务是计算这 ...
- LeetCode 664. 奇怪的打印机(区间DP)
文章目录 1. 题目 2. 解题 1. 题目 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印由 同一个字符 组成的序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. ...
- 644.奇怪的打印机(困难)
题目: 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印由 同一个字符 组成的序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给你一个字符串 s,你的任务是计算这 ...
- Strange Printer
题目描述: There is a strange printer with the following two special requirements: The printer can only p ...
- 程序功能:创建打印机类Printer,定义抽象方法Print()。 创建针式打印机类DotMatrixtPrinter和墨式打印机InkpetPrinter两个子类,修改测试类,实现该打印机打印。
程序功能:创建打印机类Printer,定义抽象方法Print(). 创建针式打印机类DotMatrixtPrinter和墨式打印机InkpetPrinter两个子类,并在各自类中重新print方法,编 ...
- Java 定义一个抽象的打印机类Printer,定义一个抽象类和具体的子类。
定义一个抽象的打印机类Printer 有属性color,有抽象方法Print()方法:定义一个其具体的ColorPrinter子类,实现Print(),当color属性为"彩色", ...
- 奇怪的打印机java
有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印由 同一个字符 组成的序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给你一个字符串 s ,你的任务是计算这个打印 ...
最新文章
- 文凭-决定的人生成败?上
- Mac下使用可执行脚本记录远程服务器账号和密码
- git clone从远程主机克隆一个版本库
- 机器学习知识点(三十四)机器学习类学习资源
- 「基本功」不可不说的Java“锁”事
- PHP - 如何解决中文乱码
- xfce中的window manager
- SQL中的存储过程中的事务处理。备忘
- 计算机机房的安全等级分,计算机机房安全等级的划分标准是什么
- com 组件调用不起来_Spring Cloud Alibaba,分布式服务调用(四)
- pycharm:There is not enough memory to perform the requested operation
- python mysql_Python-操作 MySQL
- 智能交通管理领域常用技术标准
- 声纹识别技术如何助力金融反欺诈?
- 【CLion】新手使用之编译运行单个文件
- oracle8i误删除临时表空间后的恢复
- 2021-05-11 MongoDB面试题 MongoDb中索引是什么
- 视频(图像)质量检测
- 【云原生】企业级容器管理平台Openshift介绍
- FPGA入门实验-寻迹小车的实现