[Swift]LeetCode664. 奇怪的打印機 | Strange Printer

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mfizxqwf-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There is a strange printer with the following two special requirements:git

  1. The printer can only print a sequence of the same character each time.
  2. 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.github

Example 1:微信

Input: "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb". 

Example 2:app

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.ui


有臺奇怪的打印機有如下兩個特殊要求:spa

  1. 打印機每次只能打印同一個字符序列。
  2. 每次能夠在任意起始和結束位置打印新字符,而且會覆蓋掉原來已有的字符。

給定一個只包含小寫英文字母的字符串,你的任務是計算這個打印機打印它須要的最少次數。code

示例 1:htm

輸入: "aaabbb"
輸出: 2
解釋: 首先打印 "aaa" 而後打印 "bbb"。

示例 2:blog

輸入: "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 }

Runtime: 196 ms
Memory Usage: 19.9 MB
 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 }
相關文章
相關標籤/搜索