解法一:

暴力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)的更多相关文章

  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

    LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...

  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. 手机软件没过多久就删了 APP到底得了什么病?

    直击现场 PC互联网时代正渐行渐远,移动互联网的创业浪潮汹涌而至.2014年,中国成为拥有智能手机用户最多的国家,而疯狂生长的APP正占据新的风口.据了解,目前我国主要应用商店的APP已累计超过400 ...

  2. 编译安装python

    编译安装python 1.下载python3的原码包 1.1下载到到opt目录中 cd /opt 1.2下载python3的原码包 wget https://www.python.org/ftp/py ...

  3. SYN2306B型 GPS北斗双模授时板

    SYN2306B型 GPS北斗双模授时板 产品概述 SYN2306B型GPS北斗双模授时板是由西安同步电子科技有限公司精心设计.自行研发生产的一款双模授时板卡,接收北斗或者GPS北斗混合授时卫星信号, ...

  4. Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题

    Mac上刚安装的WebStorm或PHPStorm遇到SVN版本太旧的问题: URL: svn: E155021: This client is too old to work with the wo ...

  5. 《HelloGitHub》第 39 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  6. Tido 习题-二叉树-最高分

    题目描述 老师想知道从某某同学到某某同学当中,分数最高的是多少.现在请你编程模拟老师的询问.当然,老师有时候需要更新某位同学的成绩. 输入 输入包含多组测试数据.每组输入第一行是两个正整数N和M(0& ...

  7. 我与微软的不解之缘 - 我的Insider Dev Tour 2019讲师之旅

    标题:我与微软的不解之缘 - 我的Insider Dev Tour 2019讲师之旅 作者:Lamond Lu 大家好,我是陆楠,来自北京盛安德青岛分公司,今年非常有幸作为讲师参加了微软Insider ...

  8. 【分页工具-spring boot】Mybatis PageHelper 集成Spring boot

    官方文档:https://github.com/pagehelper/pagehelper-spring-boot 1.引入包,测试过以下版本兼容性还是比较好的 <!--Mybatis-Spri ...

  9. php7中异常

    php7中新增异常错误处理 在PHP7之前的版本,对于一些错误异常是没有办法捕获的. php7中新增throwable接口,可以用来捕获一些错误 Exception,Error这实现了Throwabl ...

  10. 【半小时大话.net依赖注入】(一)理论基础+实战控制台程序实现AutoFac注入

    系列目录 第一章|理论基础+实战控制台程序实现AutoFac注入 第二章|AutoFac的常见使用套路 第三章|实战Asp.Net Framework Web程序实现AutoFac注入 第四章|实战A ...