(M)Dynamic Programming:2 Keys Keyboard
来源:互联网 发布:电脑怎么检查网络 编辑:程序博客网 时间:2024/05/22 06:19
这个题也是用的递归,回溯,不知道怎么用动态规划。类似的题用回溯的其实很多,就是这种给两种操作,求达到要求的时候操作次数最少能有几次。这道题就是,如果当前复制的东西加上当前字符串的个数到了n,就返回,如果不到n,就考虑两种情况,要么把现在所有的字符都复制再粘贴,要么还是粘贴上次复制的个数。这里我加了一个处理,如果当前复制的东西加上去之后超过了n,那么返回一个很大的数,因为不满足条件。
class Solution {public: int minSteps(int n) { if(n == 1) return 0; return minSteps(n, 1, 1, 1); } int minSteps(int n, int al_times, int pastenum, int nownum) { int x = nownum + pastenum; if(x > n) return INT_MAX; if(x == n) { return al_times +1; } else { return min(minSteps(n, al_times + 2, x, x),minSteps(n, al_times+1, pastenum, x)); } }};但是这道题也有动态规划的解法,建立一个数组dp,dp[i]存放n为i的情况下的最少次数,如果n=6,遍历n=1~5的情况,发现3能整除6,那么dp[3]+6/3次能得到6,2也能被6整除,那么dp[2]+6/2次也能得到6,遍历找到最小的就可以了。
class Solution {public: int minSteps(int n) { int *dp = new int[n+1]; for (int i = 2; i <= n; i++) { dp[i] = i; for (int j = i-1; j > 1; j--) { if (i % j == 0) { dp[i] = min(dp[i], dp[j] + (i/j)); } } } return dp[n]; }};
阅读全文
0 0
- (M)Dynamic Programming:2 Keys Keyboard
- LeetCode: -Dynamic Programming-2 Keys Keyboard[650]-复制粘贴问题
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- LeetCode-2 Keys Keyboard
- LeetCode650. 2 Keys Keyboard
- LeetCode:2 Keys Keyboard
- LeetCode650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650 2 Keys Keyboard
- 650.Two Keys Keyboard(M)
- LeetCode 650 2 Keys Keyboard
- leetcode 650. 2 Keys Keyboard
- 浓墨重彩之OpenStack-03-云计算概览(社区)
- 浓墨重彩之OpenStack-06-nova计算服务
- 解释Java中的值传递
- POJ 1797 Heavy Transportion Dijkstra变形
- 静态变量初始化的时机
- (M)Dynamic Programming:2 Keys Keyboard
- bzoj1306
- shiro整合spring
- CSDN博客编辑用html记录
- BZOJ 2177 最小曼哈顿生成树
- 持续集成
- c++面试题
- Hibernate4 save方法无法保存数据
- 【bzoj2733】【HNOI2012】永无乡