[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 ...
随机推荐
- Struts2 学习笔记(概述)
Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...
- pycharm窗口选项卡管理
1.主题 我们已经注意到Pycharm的主编辑框是基于窗口选项卡机制显示的,Pycharm选项卡多种多样,这里我们将详细介绍这种选项卡机制. 2.激活的选项卡 每当我们打开一个Python文件时op ...
- 前端视频转码flv->swf
在项目中本来是要用<video>标签插入视频,但此标签不支持flv格式,上网也是搜了很多,试了很久才成功(其他格式不是很清楚你们可以尝试看看) 所用技术 flach (Flvplayer. ...
- 基于jsp技术的校园二手交易网站
[项目介绍]基于jsp的校园二手商品交易网站系统使用jsp技术进行开发,项目主要实现了一整套的校园二手交易逻辑, 主要功能如下(包括但不限于,只列出主要功能): 管理员模块 |-----用户管理 ...
- 高并发 Nginx+Lua OpenResty系列(8)——Lua模版渲染
模版渲染 动态web网页开发是Web开发中一个常见的场景,比如像京东商品详情页,其页面逻辑是非常复杂的,需要使用模板技术来实现.而Lua中也有许多模板引擎,如目前京东在使用的lua-resty-tem ...
- 修改linux(kali)和windows双系统下默认启动系统和启动延时
我的公众号,正在建设中,欢迎关注: windows和kali双系统安装完成后kali是默认的启动系统,现将windows设置为默认启动系统并更改选择系统等待时间 1.开机时当运行到系统选择菜单时记下w ...
- Spring-Boot + MyBatis-Plus 踩坑记录
这两天在学SpringBoot+MyBatis的开发,配置开发环境和DEMO的过程中踩了很多坑,在这里记录一下. 我的开发环境是idea + JDK 1.8.0.211. 首先展示一下demo的项目整 ...
- java小数保留位数四舍五入
方法一:四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, Roundin ...
- Hadoop 学习之路(六)—— HDFS 常用 Shell 命令
1. 显示当前目录结构 # 显示当前目录结构 hadoop fs -ls <path> # 递归显示当前目录结构 hadoop fs -ls -R <path> # 显示根目录 ...
- SpringBoot(十九)_404返回统一异常处理结果
之前写过一篇统一异常处理的文章,今天测试了下如果访问一个不存在的接口,也想返回统一的错误信息,应该怎么做 1.修改application.properties文件 # 自定义404 #出现错误时, 直 ...