664:奇怪的打印机
This commit is contained in:
parent
ada24676af
commit
e3d9ea6a18
75
src/main/java/leetcode/editor/cn/StrangePrinter.java
Normal file
75
src/main/java/leetcode/editor/cn/StrangePrinter.java
Normal file
@ -0,0 +1,75 @@
|
||||
//有台奇怪的打印机有以下两个特殊要求:
|
||||
//
|
||||
//
|
||||
// 打印机每次只能打印由 同一个字符 组成的序列。
|
||||
// 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符。
|
||||
//
|
||||
//
|
||||
// 给你一个字符串 s ,你的任务是计算这个打印机打印它需要的最少打印次数。
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:s = "aaabbb"
|
||||
//输出:2
|
||||
//解释:首先打印 "aaa" 然后打印 "bbb"。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:s = "aba"
|
||||
//输出:2
|
||||
//解释:首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 100
|
||||
// s 由小写英文字母组成
|
||||
//
|
||||
// Related Topics 深度优先搜索 动态规划
|
||||
// 👍 162 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//664:奇怪的打印机
|
||||
public class StrangePrinter {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new StrangePrinter().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int strangePrinter(String s) {
|
||||
int length = s.length();
|
||||
int[][] dp = new int[length][length];
|
||||
// for (int i = 0; i < length; i++) {
|
||||
// dp[i][i] = 1;
|
||||
// }
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
dp[i][i] = 1;
|
||||
for (int j = i + 1; j < length; j++) {
|
||||
if (s.charAt(i) == s.charAt(j)) {
|
||||
dp[i][j] = dp[i][j - 1];
|
||||
} else {
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int k = i; k <= j - 1; k++) {
|
||||
min = Math.min(min, dp[i][k] + dp[k + 1][j]);
|
||||
}
|
||||
dp[i][j] = min;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[0][length - 1];
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
35
src/main/java/leetcode/editor/cn/StrangePrinter.md
Normal file
35
src/main/java/leetcode/editor/cn/StrangePrinter.md
Normal file
@ -0,0 +1,35 @@
|
||||
<p>有台奇怪的打印机有以下两个特殊要求:</p>
|
||||
|
||||
<ul>
|
||||
<li>打印机每次只能打印由 <strong>同一个字符</strong> 组成的序列。</li>
|
||||
<li>每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> ,你的任务是计算这个打印机打印它需要的最少打印次数。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaabbb"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>首先打印 "aaa" 然后打印 "bbb"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aba"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>动态规划</li></div></div>\n<div><li>👍 161</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user