[leetcode] 650. 2 Keys Keyboard (Medium)
解法一:
暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历。
注意剪枝的地方:
1、当前A数量大于目标数量,停止搜索
2、当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态。
Runtime: 8 ms, faster than 46.69% of C++ online submissions for 2 Keys Keyboard.
class Solution
{
public:
int targetNum = ;
int resNum = INT_MAX;
int minSteps(int n)
{
targetNum = n;
if (n == )
return ;
dfs(, , );
return resNum;
}
void dfs(int copy, int curNum, int times)
{
if (curNum == targetNum)
{
resNum = min(times, resNum);
return;
}
else if (curNum >= targetNum)
return;
else if (copy >= curNum)
dfs(copy, curNum + copy, times + );
else
{
dfs(curNum, curNum, times + );
dfs(copy, curNum + copy, times + );
}
}
};
解法二:
当n >= 2的时候,最优策略就是尽可能地生成n的最大因数(n / d)个A,然后进行 Copy 一次 Paste d - 1次操作,
为使n / d尽可能的大,只能使d尽可能的小,于是d从2开始循环。当找到一个d之后,我们接下来只需要解决生成n /d个A的问题,所以在循环中让n变为n / d即可。时间复杂度降低到了O(logn)。
Runtime: 0 ms, faster than 100.00% of C++ online submissions for 2 Keys Keyboard.
class Solution
{
public:
int minSteps(int n)
{
int res = ;
int d = ;
while (n > )
{
while (n % d == )
{
res += d;
n /= d;
}
d++;
}
return res;
}
};
[leetcode] 650. 2 Keys Keyboard (Medium)的更多相关文章
- [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
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
- [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 ...
随机推荐
- 如何在 Xcode 中进行 Qt 开发(可使用使用 Homebrew 来安装qt)
第一步 安装 Qt 分为两种情况: 1. 希望使用 Qt Quick 等先进Qt5技术, 2. 只需要 Qt 4.8的类库即可. 第一种, 直接去官网下载 Mac安装包, 在此不作过多说明, 开发时也 ...
- Mac OS下terminal的快捷键
时隔2年又开始使用Mac OS系统,之前的很多快捷键和常用的命令都忘记了,使用起来确实不方便,效率也低,特别是terminal下,所以对于terminal又找了一下并整理如下,希望对后来的同学也有用: ...
- windows service 之访问权限(有NetworkService和LocalSystem的区分)
最近写了一个关于从局域网的算机上下载文件的winodws service,最初认为应该没什么大的问题.通过本地的调试也没发现问题.但是当我把程序发布后发现服务报错“访问路径被拒绝”,我的第一感觉,肯定 ...
- 适配DirectFB到qt4.8.5(嵌入式Linux)
适配DirectFB到qt4.8.5 1.编译qt4.8.5 这部在qt官网上其实已经有较详细的说明,了解更多,请点击链接进入. 首先是配置选项,如下是我的配置选项: ./configure \ -p ...
- 解释一下,知乎上关于C语言奇技淫巧最火的回答。
前几天逛知乎,碰巧发现一个很火的问题. “C 语言有什么奇技淫巧?”虽然问题本身没有什么特殊的地方,但是网友的回答却是非常幽默. 下面就让我们一起来分析一下,被赞最多的答案“C 语言运算符 趋向于‘- ...
- epoll模型的探索与实践
我们知道nginx的效率非常高,能处理上万级的并发,其之所以高效离不开epoll的支持, epoll是什么呢?,epoll是IO模型中的一种,属于多路复用IO模型; 到这里你应该想到了,select, ...
- Redis 学习笔记(篇三):跳表
跳表 跳表(skiplist)是一种有序的数据结构,是在有序链表的基础上发展起来的. 在 Redis 中跳表是有序集合(sort set)的底层实现之一. 说到 Redis 中的有序集合,是不是和 J ...
- 解决安装Oracle本地可以访问客户端不能访问
现象:本地需要修改监听为localhost -->win+r--> sqlplus system/123@xxdb 可以登陆,远程客户端不能登陆:需要将监听修改为IP地址,重启监听:远程可 ...
- Flink UDF
本文会主要讲三种udf: ScalarFunction TableFunction AggregateFunction 用户自定义函数是非常重要的一个特征,因为他极大地扩展了查询的表达能力.本文除了介 ...
- Scala 学习之路(八)—— 类和对象
一.初识类和对象 Scala的类与Java的类具有非常多的相似性,示例如下: // 1. 在scala中,类不需要用public声明,所有的类都具有公共的可见性 class Person { // 2 ...