LeetCode 第650题

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

题目的意思就是在一个文本编辑器里,有个一字符A,而你的键盘上只有两个按键,复制全部粘贴, 现在要获得 nA,问你最少需要按多少次按键才能获得

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];
}
};

这个解法分为两步

  1. 查看获得 i 个A时剪贴板上A的个数 currClipBoard, 然后看以这个剪贴板进行粘贴能不能更新后面的k个A的次数。
  2. 以目前的 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的更多相关文章

  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. [LeetCode] 651. 4 Keys Keyboard 四键的键盘

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

  4. LC 650. 2 Keys Keyboard

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

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

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

  6. 650. 2 Keys Keyboard

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

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

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

  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. Spring框架Controller层(表现层)针对方法参数是Bean时HttpServletRequest绑定参数值问题解释

    在做项目的时候,有一个需求是将数据库中的信息封装到实体类返回到jsp界面 传过来的参数只是实体类的id属性,然后根据id属性去查数据库,事情就是这样,然后 结果遇到很奇怪的事情,在jsp页面中使用EL ...

  2. ROS学习记录(二)————使用smartcar进行仿真(用.xacro文件来运行rviz)

    我发现一个学习ROS系统的好网站: 创客智造http://www.ncnynl.com/ 这里面关于ROS的各个方面都有很详细的介绍. 这周,不,上周我对整个ROS是绝望的,我用一个一个下午的时间在敲 ...

  3. Singleton(单例)模式

    Singleton(单例)模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. public class Singleton { private static Singleton ourIns ...

  4. 201521123034《Java程序设计》第八周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 实验总结 答:这题是在课堂上 ...

  5. 201521123085 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 实验总结 要注意到在删除元素 ...

  6. windows消息机制与实例

    windows发送窗口消息 所需工具:spy++,visual studio 2017,c#语言 技术路线:首先通过spy++获得所要操纵的窗口的句柄,函数的原型声明为: [DllImport(&qu ...

  7. 201521123104《JAVA程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 1. 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出 ...

  8. mybatis教程:入门>>精通>>实战

    以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活.性能也比hibernate好.而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记.后来项 ...

  9. Mysql常用命令大全

    1.连接Mysql 格式: mysql ­h主机地址 ­u用户名 -p用户密码 2.1 创建数据库 命令:create database <数据库名> 例1:建立一个名为xhkdb的数据库 ...

  10. grep与正则表达式基础

    目录 grep 正则表达式 grep用法简介 我们介绍GREP的用法,主要用于匹配行,我们借助下面的正则表达式来介绍如何使用grep,还有就是正则表达式在linux中是极为重要的一部分. 命令:gre ...