[LeetCode] 650. 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).Paste: You can paste the characters which are copied last time.
Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.
Example 1:
Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.
Note:
- The
nwill be in the range [1, 1000].
这道题只给了我们两个按键,如果只能选择两个按键,那么博主一定会要复制和粘贴,此二键在手,天下我有!!!果然,这道题就是给了复制和粘贴这两个按键,然后给了一个A,目标时利用这两个键来打印出n个A,注意复制的时候时全部复制,不能选择部分来复制,然后复制和粘贴都算操作步骤,问打印出n个A需要多少步操作。对于这种有明显的递推特征的题,要有隐约的感觉,一定要尝试递归和 DP。递归解法一般接近于暴力搜索,但是有时候是可以优化的,从而能够通过 OJ。而一旦递归不行的话,那么一般来说 DP 这个大杀器都能解的。还有一点,对于这种题,找规律最重要,DP 要找出状态转移方程,而如果无法发现内在的联系,那么状态转移方程就比较难写出来了。所以,从简单的例子开始分析,试图找规律:
当n = 1时,已经有一个A了,不需要其他操作,返回0
当n = 2时,需要复制一次,粘贴一次,返回2
当n = 3时,需要复制一次,粘贴两次,返回3
当n = 4时,这就有两种做法,一种是需要复制一次,粘贴三次,共4步,另一种是先复制一次,粘贴一次,得到 AA,然后再复制一次,粘贴一次,得到 AAAA,两种方法都是返回4
当n = 5时,需要复制一次,粘贴四次,返回5
当n = 6时,需要复制一次,粘贴两次,得到 AAA,再复制一次,粘贴一次,得到 AAAAAA,共5步,返回5
通过分析上面这6个简单的例子,已经可以总结出一些规律了,首先对于任意一个n(除了1以外),最差的情况就是用n步,不会再多于n步,但是有可能是会小于n步的,比如 n=6 时,就只用了5步,仔细分析一下,发现时先拼成了 AAA,再复制粘贴成了 AAAAAA。那么什么情况下可以利用这种方法来减少步骤呢,分析发现,小模块的长度必须要能整除n,这样才能拆分。对于 n=6,我们其实还可先拼出 AA,然后再复制一次,粘贴两次,得到的还是5。分析到这里,解题的思路应该比较清晰了,找出n的所有因子,然后这个因子可以当作模块的个数,再算出模块的长度 n/i,调用递归,加上模块的个数i来更新结果 res 即可,参见代码如下:
解法一:
class Solution {
public:
int minSteps(int n) {
if (n == ) return ;
int res = n;
for (int i = n - ; i > ; --i) {
if (n % i == ) {
res = min(res, minSteps(n / i) + i);
}
}
return res;
}
};
下面这种方法是用 DP 来做的,我们可以看出来,其实就是上面递归解法的迭代形式,思路没有任何区别,参见代码如下:
解法二:
class Solution {
public:
int minSteps(int n) {
vector<int> dp(n + , );
for (int i = ; i <= n; ++i) {
dp[i] = i;
for (int j = i - ; j > ; --j) {
if (i % j == ) {
dp[i] = min(dp[i], dp[j] + i / j);
}
}
}
return dp[n];
}
};
上面的解法其实可以做一丢丢的优化,在遍历j的时候,只要发现了第一个能整除i的j,直接可以用 dp[j] + i/j 来更新 dp[i],然后直接 break 掉j的循环,之后的j就不用再考虑了,可能是因为因数越大,其需要的按键数就越少吧,参见代码如下:
解法三:
class Solution {
public:
int minSteps(int n) {
vector<int> dp(n + );
for (int i = ; i <= n; ++i) {
dp[i] = i;
for (int j = i - ; j > ; --j) {
if (i % j == ) {
dp[i] = dp[j] + i / j;
break;
}
}
}
return dp[n];
}
};
下面我们来看一种省空间的方法,不需要记录每一个中间值,而是通过改变n的值来实时累加结果res,参见代码如下:
解法四:
class Solution {
public:
int minSteps(int n) {
int res = ;
for (int i = ; i <= n; ++i) {
while (n % i == ) {
res += i;
n /= i;
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/650
类似题目:
Broken Calculator
参考资料:
https://leetcode.com/problems/2-keys-keyboard/
https://leetcode.com/problems/2-keys-keyboard/discuss/105899/Java-DP-Solution
[LeetCode] 650. 2 Keys Keyboard 两键的键盘的更多相关文章
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [leetcode] 650. 2 Keys Keyboard (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
- LC 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- 【LeetCode】650. 2 Keys Keyboard 只有两个键的键盘(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 素数分解 日期 题目地址:https://le ...
- 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- 650. 2 Keys Keyboard复制粘贴的次数
[抄题]: Initially on a notepad only one character 'A' is present. You can perform two operations on th ...
随机推荐
- 【shell脚本】将三个数字进行升序排序===numSort.sh
从命令输入三个数字进行升序排序(冒泡排序) 原理:比较两个相邻的元素,将值大的元素交换至右端. 脚本内容: [root@VM_0_10_centos shellScript]# cat numSort ...
- Flink之state processor api实践
前不久,Flink社区发布了FLink 1.9版本,在其中包含了一个很重要的新特性,即state processor api,这个框架支持对checkpoint和savepoint进行操作,包括读取. ...
- DNA Sorting POJ - 1007
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 114211 Accepted: 45704 De ...
- 常见的几种 Normalization 算法
神经网络中有各种归一化算法:Batch Normalization (BN).Layer Normalization (LN).Instance Normalization (IN).Group No ...
- redis的3种过期键删除策略
Redis的过期键的过期时间都保存在过期字典中,过期键的删除策略有三种,分别是定时删除.惰性删除和定期删除. 定时删除 定时删除策略,是指在设置键的过期时间的同时,创建一个定时器,让定时器在键的过期时 ...
- QT+OpenGL(02)-- zlib库的编译
1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入 zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...
- 写入文件writelines 换行问题
知识点:在python中没有数组的概念,有列表.元组.字典的概念 问题描述: 在写循环语句的时候,我需要把输出的列表存放到文件上,但是如果没有换行的话,存下的文件就是一坨的字. 所以在存入文件的时候就 ...
- tf.reduce_sum() and tf.where()的用法
import tensorflow as tfimport numpy as npsess=tf.Session()a=np.ones((5,6))c=tf.cast(tf.reduce_sum(a, ...
- JPA笔记4 ManyToMany
package many_to_many; import java.util.HashSet; import java.util.Set; import javax.persistence.Entit ...
- MySQL 的一些批处理
执行 SQL 脚本文件(https://blog.csdn.net/vebasan/article/details/7619911): mysql –u root –p 123456 < scr ...