[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 ...
随机推荐
- 使用 Napa 创建并调试一个 Office 内容应用 – Hello World
原文地址:http://simpeng.net/office-add-in/%e4%bd%bf%e7%94%a8-napa-%e5%88%9b%e5%bb%ba%e5%b9%b6%e8%b0%83%e ...
- C++中代理类和句柄类
指针是 C 与其他语言区别的重要特征之一,在 C++ 中,指针也被广泛运用,我们通过指针实现多态.然而,众所周知,指针的使用必须小心,否则很容易造成内存泄漏 Memory Leak.当我们有几个指针指 ...
- qt中采用宽带speex进行网络语音通话实验程序
qt中采用宽带speex进行网络语音通话实验程序 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN8 开发环境:Qt5 3.1. ...
- SpringCloud微服务架构升级总结
一.背景 1.1 应用系统的架构历史 1.2 什么是微服务? 起源:微服务的概念源于 2014 年 3 月 Martin Fowler 所写的一篇文章“Microservices”.文中内容提到:微服 ...
- android 写文件到sd卡问题小记
android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...
- 分享android ADT百度云盘下载地址
由于android官网经常无法打开,特意把最新的android ADT和SDK放到了百度云盘进行了分享,目录中包含Windows和Macbook两种平台的版本,请自行选择: http://pan.ba ...
- 基于python语言的自动化测试中生成html的测试报告时HtmlTestRunner模块常见问题
一.导入了HTMLTestRunner模块,报错:No module named StringIO,在python3.x中确实没有,在第94行引入的名称改成import io,539行要改成self. ...
- Anaconada安装
目录 Anaconda介绍 Anaconda下载 安装Anaconda 配置环境变量 管理虚拟环境 activate 切换环境 卸载环境 关于环境总结 安装第三方包 卸载第三方包 查看环境包信息 导入 ...
- ZooKeeper学习之路(四)—— Java 客户端 Apache Curator
一.基本依赖 Curator是Netflix公司开源的一个Zookeeper客户端,目前由Apache进行维护.与Zookeeper原生客户端相比,Curator的抽象层次更高,功能也更加丰富,是目前 ...
- spring 5.x 系列第10篇 —— 整合mongodb (代码配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 配置文件位于com.heibaiying. ...