[抄题]:

Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

  1. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
  2. 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'.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为是dp数组,结果只需要稍微除一下就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. totalSteps += singleSteps; 需要一直相加来积累一次性能走的最长步数,否则要走很多次

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

需要while循环一直相加的时候要注意点

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int minSteps(int n) {
//initialization: totalSteps = 0
int totalSteps = 0; //for loop for n
for (int singleSteps = 2; singleSteps <= n; singleSteps++) {
while (n % singleSteps == 0) {
totalSteps += singleSteps;
n /= singleSteps;
}
} return totalSteps; /*
singleSteps 2 2
totalSteps 2 4
n 2 1
*/
}
}

650. 2 Keys Keyboard复制粘贴的次数的更多相关文章

  1. 651. 4 Keys Keyboard复制粘贴获得的最大长度

    [抄题]: Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on scre ...

  2. [LeetCode] 650. 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  3. [leetcode] 650. 2 Keys Keyboard (Medium)

    解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...

  4. 【LeetCode】650. 2 Keys Keyboard 只有两个键的键盘(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 素数分解 日期 题目地址:https://le ...

  5. LC 650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  6. LeetCode 650 - 2 Keys Keyboard

    LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...

  7. 650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  8. [LeetCode] 651. 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

  9. [LeetCode] 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

随机推荐

  1. hsdfz -- 6.17 -- day2

    今日依旧康复…… 当天晚上被老师拉去小吃街了,晚上回来精力憔悴,所以并没有当天写 反正就惨,因为估错复杂度,期望得分100分最后结果20分 (我的复杂度是nlog^2n的,正确性有保障,稳! 事后:还 ...

  2. 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf

    ========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...

  3. Object.is()

  4. Linux内核分析第六次作业

    分析system_call中断处理过程 一.先在实验楼的虚拟机中MenuOs增加utsname和utsname-asm指令. 具体实现如下: 1.克隆最新新版本的menu,之后进入menu 2.进入t ...

  5. MySQL内存使用查看方式

    使用版本:MySQL 5.7 官方文档 在performance_schema有如下表记录内存使用情况 mysql> show tables like '%memory%summary%'; + ...

  6. 动态规划经典——最长公共子序列问题 (LCS)和最长公共子串问题

    一.最长公共子序列问题(LCS问题) 给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子序列,并返回其长度.例如: A = "HelloWorld"    B = & ...

  7. 常用http/https以及socks5代理总结

    代理 格式 # 设置http代理 export http_proxy= # 设置https代理 export HTTPS_PROXY= # 设置ftp代理 export FTP_PROXY= # 同时 ...

  8. Net-Snmp工具(学习SNMP的工具,开源项目)简单使用

    https://blog.csdn.net/mrzhangzifu/article/details/77882371 Net-Snmp工具的安装与配置  操作系统:Ubuntu16.4  软件版本:n ...

  9. CIF 搜索逻辑

    test code #include <cstddef> class CIF { }; template <typename OBJ> class CList { public ...

  10. java.lang.Integer源码浅析

    Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...