Dynamic Programming-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
n
will be in the range [1, 1000].
class Solution {
public:
int minSteps(int n) {
vector<int> dp(n+,0x7FFFFFFF); dp[] = dp[] = ; for(int i=;i<n+;++i){
for(int j=;j<i;++j){
if(i%j==){
dp[i] = min(dp[i], dp[j]+i/j);
}
}
}
return dp[n];
}
};
Dynamic Programming-650. 2 Keys Keyboard的更多相关文章
- [LeetCode] 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 (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- 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
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
- 650. 2 Keys Keyboard复制粘贴的次数
[抄题]: Initially on a notepad only one character 'A' is present. You can perform two operations on th ...
- 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 ...
- [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 ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
随机推荐
- Django开发问题及解决方法汇总
1. manage.py@MxOnline > makemigrations users manage.py@MxOnline > migrate users 2. 操作django的ad ...
- Loadrunner脚本回放无法准确定位欲删除元素
Loadrunner脚本回放无法准确定位欲删除元素 问题: loadrunner脚本回放无法准确定位欲删除元素 详细: 我司ocrm系统,我的工作台菜单->我的综合工作台子页面下,工作日志页面删 ...
- 关闭文件流--fclose,
头文件:#include<stdio.h> 函数原型:int fclose(FILE *fp) 参数说明:fp将被关闭的文件指针 返回值:成功返回0,失败返回EOF宏.
- struts2用到的jar有那些
struts2.0 lib/antlr-2.7.6.jarlib/struts2-core-2.0.14.jarlib/struts2-spring-plugin-2.0.14.jarlib/free ...
- hdu-1066(大数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1066 思路:统计2的个数,如果遇到5,就抵消,最后求和加上为来得及抵消的2的个数. 参考文章:http ...
- MySQL通过游标来实现通过查询记录集循环
/*我们有时候会遇到需要对 从A表查询的结果集S_S 的记录 进行遍历并做一些操作(如插入),且这些操作需要的数据或许部分来自S_S集合*//*临时存储过程,没办法,不能直接在查询窗口做这些事.*/d ...
- Mybatis实现原理探究-实现部分Mybatis功能(上)
一.前言: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简 ...
- DIV+CSS 中的 overflow:hidden
overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解. 一提到清除浮动,我们就会想到另外一个CSS样式 ...
- WriteableBitmap(一)
通常,WPF中的位图是不可变的.不可变的位图非常有效,如果您希望进行大量的动态更改,创建和销毁它们的开销会变得非常昂贵.在这种情况下,您需要一些更灵活的东西——WriteableBitmap. Wri ...
- Android中Activity启动过程探究
首先追溯到Activity的启动,随便启动一个自己写的demo项目,使用DDMS进行debug标记,然后在Debug中把主线程暂停,可以看到调用栈.如下图所示: 于是我们先看android.app.A ...