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
nwill 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的更多相关文章
- [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 ...
- 【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 ...
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
随机推荐
- Windows下Python IDLE设置
Windows下安装Python 2.7.5,发现IDLE是个不错的IDE,可以编辑.运行, 希望与.py文件关联起来,作为编辑器使用,经过尝试,找到了一个方法. 打开注册表,找到\KEY_CLA ...
- DB2中的数据类型
DB2中的数据类型DB2内置数据类型可以分成数值型(numeric).字符串型(character string).图形字符串(graphic string).二进制字符串型(binary strin ...
- 求N的阶乘N!中末尾0的个数
求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...
- jquery panel加载(dialog加载类似)
项目情况: 主页面用引用了公共头文件(包含easui.min.js),使用easyui的dialog(href方式)打开窗口(被打开的窗口页面是纯html片段,无body元素等,也引入了公共头文件), ...
- VC6.0 OpenGL环境配置及编程基础
1.一般情况下VC并不携带glut,需要到opengl官网下载,下载地址 http://www.opengl.org/resources/libraries/glut/glut37.zip 解压后 打 ...
- 【Jsoup】Jsoup解析Html标签(Java后台解析)
中文API网站(下载地址): http://www.open-open.com/jsoup/ 有时候编辑器传到后台的内容是带Html标签的,或者有时候需要形成一个完整的Html文档,也或者需要解析其中 ...
- 2018.09.15点名器(简单dp)
描述 Ssoier在紧张的学习中,杜老师每天给他们传授精妙的知识. 杜老师为了活跃气氛,设计了一个点名器,这个点名器包含一个长度为M的数组(下标1开始),每个元素是一个oier的名字,每次点名的时候, ...
- [转载][翻译]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[2]
Golang作为一个略古怪而新的语言,有自己一套特色和哲学.从其他语言转来的开发者在刚接触到的时候往往大吃苦头,我也不例外.这篇文章很细致地介绍了Golang的一些常见坑点,读完全篇中枪好多次.故将其 ...
- IP之ALTDDIO_in仿真
需要添加altera_mf库,才可以仿真. 上升沿输出,把前一个时钟的数据输出来. `timescale 1 ns/ 1 ns; module altddio_in_ip_tb; reg rst; r ...
- DIV+CSS实战(三)
一.说明 在上篇博客<DIV+CSS实战(二)>中,实现了头部以及Tab标签卡,下面开始实现内容区域,要实现的效果如下: 二.内容最外层的设计(边框) 给最外层加边框,并且设置高度随着里面 ...