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'.

Note:

  1. The n will be in the range [1, 1000].

Approach #1: DP. [Java]

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] = dp[j] + (i/j);
break;
}
}
} return dp[n];
}
}

  

Approach #2: Greedy. [C++]

    public int minSteps(int n) {
int s = 0;
for (int d = 2; d <= n; d++) {
while (n % d == 0) {
s += d;
n /= d;
}
}
return s;
}

  

Analysis:

We look for a divisor d so that we can make d copies of (n / d) to get n. The process of making d copies takes d steps (1 step of copy All and d-1 steps of Paste)

We keep reducing the problem to a smaller one in a loop. The best cases occur when n is decreasing fast, and method is almost O(log(n)). For example, when n = 1024 then n will be divided by 2 for only 10 iterations, which is much faster than O(n) DP method.

The worst cases occur when n is some multiple of large prime, e.g. n = 997 but such cases are rare.

Reference:

https://leetcode.com/problems/2-keys-keyboard/discuss/105897/Loop-best-case-log(n)-no-DP-no-extra-space-no-recursion-with-explanation

https://leetcode.com/problems/2-keys-keyboard/discuss/105899/Java-DP-Solution

650. 2 Keys Keyboard的更多相关文章

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

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

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

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

  3. LC 650. 2 Keys Keyboard

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

  4. LeetCode 650 - 2 Keys Keyboard

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

  5. 650. 2 Keys Keyboard复制粘贴的次数

    [抄题]: Initially on a notepad only one character 'A' is present. You can perform two operations on th ...

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

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

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

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

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

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

  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. Golang之Mysql事务

    Mysql事务 )原子性 )一致性 )隔离性 )持久性 示例代码 package main import ( "fmt" _ "github.com/go-sql-dri ...

  2. 80% UI 初学者走过的弯路,你走了几条?

    关于UI 对于初学UI设计的人而言,可能对UI具体是做什么,或者自己是否能顺利转行胜任这样的岗位存在一定的顾虑,今天我们就来重点说说UI是做什么的,以及学UI到有哪些需要避免的弯路. 1.UI设计是做 ...

  3. Zookeeper 系列(三)Zookeeper API

    Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...

  4. 开启多个tomcat 注意

    1. 将tomcat 复制到另一个文件夹 2. 更改 tomcat 文件夹中 conf/ server.xml 文件 .共3个地方. 1.  shutdown  的port 2.  connector ...

  5. 百度词汇检索,计算PMI值

    '''词汇检索百度返回值,并且计算PMI值的类''' from bs4 import BeautifulSoup import requests import re import pandas as ...

  6. 构造函数constructor 与析构函数destructor(五)

    我们知道当调用默认拷贝构造函数时,一个对象对另一个对象初始化时,这时的赋值时逐成员赋值.这就是浅拷贝,当成员变量有指针时,浅拷贝就会在析构函数那里出现问题.例如下面的例子: //test.h #ifn ...

  7. 2018.10.19 NOIP模拟 比特战争(kruskal)

    传送门 考完发现是sbsbsb题啊. 直接考虑优化状压的转移. 可以证明最优解一定在求最小生成树的时候取得. 因此再最小生成树时维护一下连通块的最值统计答案就行了. 代码

  8. 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)

    [描述] 小石头喜欢看电影,选择有 N 部电影可供选择,每一部电影会在一天的不同时段播 放.他希望连续看 L 分钟的电影.因为电影院是他家开的,所以他可以在一部电影播放过程中任何时间进入或退出,当然他 ...

  9. flask_模板

    由于python中生成html比较繁琐,所以flask自动为你配置好jinjia2模板.下面我们开始学习模板应用吧~ 1.编写microblog模块 注:(1)这里为了渲染模板,我们从Flask导入了 ...

  10. 多网卡下对ServerSocket以TCP协议绑定IP和端口的测试

    一.引言:之前开发TCP协议的程序(C#里是Socket为主)都是基于主机上只有一个IP的,后来项目里涉及到了主机需要同时连接内外和外网的情况,在该主机上部署着一套WCS系统和一套WMS系统:WCS系 ...