leetcode650—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].
想法:DP方式,状态转移表达时候result[i] = min(result[i],result[j]+i/j)
class Solution {
public:
int minSteps(int n) {
vector<);
result[] = ;
; i <= n ; i++){
result[i] = i;
; j < i/ ; j++){
){
result[i] = min(result[i],result[j] + i / j);
}
}
}
return result[n];
}
};
leetcode650—2 Keys Keyboard的更多相关文章
- leetcode650 2 Keys Keyboard
思路: 把给定的数分解质因子之后,对于每一个质因子x,都需要x次操作(一次copy all操作和x-1次paste),所以答案就是对分解后的所有质因子求和. 实现: class Solution { ...
- [LeetCode] 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解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- Leetcode 之 Keys Keyboard
1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...
- LeetCode 4 Keys Keyboard
原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...
- [leetcode] 650. 2 Keys Keyboard (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- [LeetCode] 650. 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
随机推荐
- How to write a professional email?
Advantages and Disadvantages of Email communication compared with Face-To-Face communication: Emai ...
- [Android]Java中点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
- 对象属性键值[key]属性问题
1,obj[key]此时key代表是循环中的一个变量. var obj = {key:3,key1:1,key2:2,key3:3,toSting:4}; for (key in obj) { con ...
- centos7下docker发布第一个微服务应用(Eureka)
1.在windows下打包 微服务应用通过maven进行打包,在项目的pom.xml执行mvn clean package,或者直接通过idea或者eclipse进行maven打包 之上操作将在项目的 ...
- git使用相关记录
上传github操作记录:https://blog.csdn.net/pql925/article/details/72772660 git提交仓库相关:https://blog.csdn.net/M ...
- C3p0 的一个异常
转的,异常如下: NewPooledConnection - com.mchange.v2.c3p0.impl.NewPooledConnection@1285252 closed by a clie ...
- windows10 java环境变量设置
由于安装的是jdk1.8所以不需要配置classpath了,只需要配置java_home和path即可. 我的电脑 -> 右键属性 -> 高级系统设置 -> 高级 -> 环境变 ...
- 3.1 - Apps or metadata that mentions the name of any other mobile platform will be rejected
3.1 - Apps or metadata that mentions the name of any other mobile platform will be rejected3.1 Detai ...
- Python 排错UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法
Python UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法 by:授客 QQ:1033553122 错误描述: py ...
- 在Android Native层中创建Java虚拟机实例
前言 Android应用中JNI代码,是作为本地方法运行的.而大部分情况下,这些JNI方法均需要传递Dalvik虚拟机实例作为第一个参数.例如,你需要用虚拟机实例来创建jstring和其他的Java对 ...