只有两个键的键盘

最初在一个记事本上只有一个字符 'A'。你每次可以对这个记事本进行两种操作:

1.Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的)。

2.Paste (粘贴) : 你可以粘贴你上一次复制的字符。

给定一个数字 n 。你需要使用最少的操作次数,在记事本中打印出恰好 n 个 'A'。输出能够打印出 n 个 'A' 的最少操作次数。

示例 1:

输入: 3

输出: 3

解释:

最初, 我们只有一个字符 'A'。

第 1 步, 我们使用 Copy All 操作。

第 2 步, 我们使用 Paste 操作来获得 'AA'。

第 3 步, 我们使用 Paste 操作来获得 'AAA'。

说明:

1.n 的取值范围是 [1, 1000] 。


分析

如果每次Paste 'A',显然操作次数就是n,那么怎么减少次数呢?这里分为两种情况:

1.n为质数,无论剪切板里有多少个‘A'(除了1个'A'的情况),最终都无法得到n个'A',所以最少操作次数为n;

2.n为合数,得到最少操作次数的情况发生在已经有(n / i)个'A',其中i为最小的能整除n的数(i大于1),说明只要操作i次就能得到n个'A'。

暴力递归

class Solution {
public int minSteps(int n) { if(n == 1) return 0;
int i = 2;
while(n % i != 0) i++;
if(i == n) return n;
return i + minSteps(n / i);
}
}

递归转迭代

class Solution {
public int minSteps(int n) { int res = 0;
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
res += i;
n /= i;
}
}
return res;
}
}

动态规划

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 = 2; j < i; j++){
if(i % j == 0){
dp[i] = Math.min(dp[i], dp[i / j] + j);
}
}
}
return dp[n];
}
}

【LeetCode】650. 只有两个键的键盘的更多相关文章

  1. Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)

    650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...

  2. Leetcode 650.只有两个键的键盘

    只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). Past ...

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

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

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

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

  5. [Swift]LeetCode650. 只有两个键的键盘 | 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 两键的键盘

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

  7. [LeetCode] 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. 如何使用alt键+数字键盘上的数字键打出特殊符号

    如何使用alt键+数字键盘上的数字键打出特殊符号 有时当我需要画示意图说明一个问题,但是苦于没有合适的符号,因此,这篇博文将简单介绍一些特殊的符号方便自己以及大家使用. 实现原理很简单:所有的字符(包 ...

随机推荐

  1. Go-获取变量数据类型

    package main import ( "fmt" "reflect" //这个包里的TypeOf方法获取变量数据类型 ) func main(){ b : ...

  2. GO-逻辑判断(if,else if,else,switch)

    一.if逻辑判断 package main import "fmt" func main() { var a =10; if a>10 { //大括号前不能回车 fmt.Pr ...

  3. Java - 运算符 方法

    本位是复习笔记,不适合零基础 赋值运算符 变量 名称 = 值/表达式 ; 需要注意的是,赋值运算符的计算是按照从右往左的; 注意: 1.在使用赋值符号时,必须保证左侧的类型范围大于等于右侧产生的类型 ...

  4. Eclipse 的快捷键

    1. 代码折叠的快捷键,默认是: Ctrl+Shift+Numpad_Divede(小键盘的/号) Ctrl+Shift+Numpad_Multiply(小键盘的*号) 2.删除一行:Ctrl+D 3 ...

  5. HTML实例之搜索栏(附源码)

    1. 简书类 实现效果 html代码 <div class="container"> <form action="" class=" ...

  6. js基础——错误处理

    一:错误捕获 1.try-catch 语句(错误捕获) try{ //这里放置可能出现问题的代码 }catch(error){ //错误发生时执行的代码 console.log(error.name) ...

  7. grant localhost and % for mysql

  8. 单词倒序(java)

    如何将一串单词组成的字符串倒序呢?如:" we go to school" 变成"school to go we "java代码实现: public stati ...

  9. Redis—配置文件详解

    https://www.cnblogs.com/shizhengwen/p/9283973.html https://www.cnblogs.com/yangy608/p/4443665.html h ...

  10. Linux下搭建及配置禅道服务器详细过程-包含软件资源-Dotest-董浩

    Linux环境下搭建禅道管理工具 1:百度云盘下载: 禅道--链接:https://pan.baidu.com/s/1Stu7nOZVIPO5TnpJWjWtiQ 提取码:dnik CentOs操作系 ...