LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题
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'.
题目的意思就是在一个文本编辑器里,有个一字符A,而你的键盘上只有两个按键,复制全部和粘贴, 现在要获得 n 个A,问你最少需要按多少次按键才能获得
n的范围为[1, 1000];
这道题在Leetcode上的分类为动态规划,那么我就尝试使用动态规划,自底向上递推的做。
class Solution {
public:
int minSteps(int n) {
vector<int> dp(n + 1, 0x7FFFFFFF);
vector<int> clipBoard(n + 1, 0);
dp[1] = 0;
dp[2] = 2;
clipBoard[2] = 1;
for (int i = 2; i <= n; ++i) {
int currClipBoard = clipBoard[i];
int j = i;
for (int j = i, step = 1; j + currClipBoard <= n; ++step, j+=currClipBoard) {
if (dp[j + currClipBoard] > dp[i] + step) {
dp[j + currClipBoard] = dp[i] + step;
clipBoard[j + currClipBoard] = currClipBoard;
}
}
if (i * 2 <= n && dp[i * 2] > dp[i] + 2) {
dp[i * 2] = dp[i] + 2;
clipBoard[i * 2] = i;
}
}
return dp[n];
}
};
这个解法分为两步
- 查看获得 i 个A时剪贴板上A的个数 currClipBoard, 然后看以这个剪贴板进行粘贴能不能更新后面的k个A的次数。
- 以目前的 i 个A为起点,复制 i 个A到剪贴板,看能否更新后续的 i * 2 个 A的次数
dp[n] 就是结果。
最终结果是通过了。
然后在Discussion区中发现了一个更加高明的做法,代码也很简洁。
class Solution {
public:
int minSteps(int n) {
if (n == 1) return 0;
for (int i = 2; i < n; i++)
if (n % i == 0) return i + minSteps(n / i);
return n;
}
};
我尝试解释这段代码的含义:
不难证明,如果n为质数,我们只能够通过n次操作来获得n个A,例如
n = 2 : 复制, 粘贴
n = 3 : 复制, 粘贴,粘贴。
n = 5 : 复制, 粘贴 *4
因为 n 为质数时,它的因数只有 1 和 本身,所以 n 为质数时,只能够通过复制 第一个 A, 然后通过 n -1 次粘贴来获得。
所以我们可以看出,想要把 x 个A扩展成 kx 个的话,如果 k 为 素数的话,最少需要 k 个按键操作才能实现。
那如果 n 不为 素数呢?
我们可以 把 n 划分成两个数的乘积 n = a * b;
那么 f(n) = f(a) + b;
那么b要怎么取呢?
我们假设b取的是合数(即可以拆成两个或多个的乘积)。我们假设b能够拆成2个素数的乘积 (b = m*n)
由于所有的素数都大于1。对于下面的公式是恒成立的:
m * n >= m + n;
所以 对于 b的选择来说,我们应该选择素数。
即对于b为合数的情况
f(a) + b > f(a) + m + n = f(a*m) + n
b的选择就已经很明显了。
于是问题转化为对n进行分解,分解成k个素数的乘积,然后求这k个素数的和。
这也是Discussion中算法的原理。
LeetCode 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 ...
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- 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 只有两个键的键盘(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 素数分解 日期 题目地址:https://le ...
- 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- 650. 2 Keys Keyboard复制粘贴的次数
[抄题]: Initially on a notepad only one character 'A' is present. You can perform two operations on th ...
- [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 ...
随机推荐
- Ajax异步请求模板
$.ajax({ url: '', type: 'post', data: {'id':id}, dataType: 'json', success: function(data,statusText ...
- JAVA 文件编译执行与虚拟机(JVM)简单介绍
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytpo3 java程序的内存分配 JAVA 文件编译执行与虚拟机(JVM)介绍 ...
- JAVA HashMap的实现原理
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt359 1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存 ...
- 【小白成长撸】--Fibonacci
/*程序的版权和版本声明部分: *Copyright(c) 2016,电子科技大学本科生 *All rights reserved. *文件名:Fibonacci *程序作用:计算菲薄拉稀数列 *作者 ...
- IntelliJ IDEA使用(一)基本设置与类、方法模板设置
其实之前一直开发都是在使用的是Eclipse,但是最近在做Maven项目的时候要用IntelliJ IDEA,据说这个idea功能非常的强大,最近在使用的时候发现如果适应的真的是非常的强大.感觉是比E ...
- 数据绑定技术一:GridView控件
在网站或应用程序中,要显示数据信息,可用到ASP.NET提供的数据源控件和能够显示数据的控件. 一.数据源控件 数据源控件用于连接数据源.从数据源中读取数据以及把数据写入数据源. 1.数据源控件特点 ...
- YYHS-NOIP2017SummerTraining0914-问题 A: 组合数问题
题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1, 2, 3)三个物品中选择两个物品可以有(1, 2),(1, 3),(2, 3)这三种选择方法.根据组合数的定义 ...
- DevTool开发者工具
DevTool开发者工具 chrome的开发者工具可以说是十分强大了,是web开发者的一大利器,作为我个人而言平时用到的几率很大,相信大家也很常见,但是不要仅仅停留在点选元素看看样式的层面上哦,跟着我 ...
- 201521123083《Java程序设计》第11周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 上周这张图没理解完,继续 2. 书面作业 本次PTA作业题集多线程 1互斥访问与同步访问完成题集4-4(互斥访问) ...
- 团队作业8----第二次项目冲刺(beta阶段)5.22
Day4-05.22 1.每日会议 会议内容: 1.帮助新成员进一步了解项目. 2.陈鑫龙说明昨日任务的完成情况. 3.组长林乔桦安排今日的任务. 讨论照片: 2.任务分配情况: 每个人的工作分配表: ...