650:只有两个键的键盘

This commit is contained in:
huangge1199@hotmail.com 2021-09-19 23:23:20 +08:00
parent ab85e33f82
commit be5fb86ee6
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,68 @@
//最初记事本上只有一个字符 'A' 你每次可以对这个记事本进行两种操作
//
//
// Copy All复制全部复制这个记事本中的所有字符不允许仅复制部分字符
// Paste粘贴粘贴 上一次 复制的字符
//
//
// 给你一个数字 n 你需要使用最少的操作次数在记事本上输出 恰好 n 'A' 返回能够打印出 n 'A' 的最少操作次数
//
//
//
// 示例 1
//
//
//输入3
//输出3
//解释
//最初, 只有一个字符 'A'
// 1 , 使用 Copy All 操作
// 2 , 使用 Paste 操作来获得 'AA'
// 3 , 使用 Paste 操作来获得 'AAA'
//
//
// 示例 2
//
//
//输入n = 1
//输出0
//
//
//
//
// 提示
//
//
// 1 <= n <= 1000
//
// Related Topics 数学 动态规划 👍 396 👎 0
package leetcode.editor.cn;
//650:只有两个键的键盘
class TwoKeysKeyboard {
public static void main(String[] args) {
//测试代码
Solution solution = new TwoKeysKeyboard().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minSteps(int n) {
int[] arrs = new int[n + 1];
for (int i = 2; i <= n; ++i) {
arrs[i] = i;
for (int j = 1; j * j <= i; ++j) {
if (i % j == 0) {
arrs[i] = Math.min(arrs[i], arrs[j] + i / j);
arrs[i] = Math.min(arrs[i], arrs[i / j] + j);
}
}
}
return arrs[n];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,38 @@
<p>最初记事本上只有一个字符 <code>'A'</code> 。你每次可以对这个记事本进行两种操作:</p>
<ul>
<li><code>Copy All</code>(复制全部):复制这个记事本中的所有字符(不允许仅复制部分字符)。</li>
<li><code>Paste</code>(粘贴):粘贴<strong> 上一次 </strong>复制的字符。</li>
</ul>
<p>给你一个数字&nbsp;<code>n</code> ,你需要使用最少的操作次数,在记事本上输出 <strong>恰好</strong>&nbsp;<code>n</code>&nbsp;<code>'A'</code> 。返回能够打印出&nbsp;<code>n</code>&nbsp;<code>'A'</code> 的最少操作次数。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>3
<strong>输出:</strong>3
<strong>解释:</strong>
最初, 只有一个字符 'A'。
第 1 步, 使用 <strong>Copy All</strong> 操作。
第 2 步, 使用 <strong>Paste </strong>操作来获得 'AA'。
第 3 步, 使用 <strong>Paste</strong> 操作来获得 'AAA'。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li><li>动态规划</li></div></div><br><div><li>👍 396</li><li>👎 0</li></div>