[LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys:
Key 1: (A): Print one 'A' on screen.
Key 2: (Ctrl-A): Select the whole screen.
Key 3: (Ctrl-C): Copy selection to buffer.
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.
Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of 'A' you can print on screen.
Example 1:
Input: N = 3
Output: 3
Explanation:
We can at most get 3 A's on screen by pressing following key sequence:
A, A, A
Example 2:
Input: N = 7
Output: 9
Explanation:
We can at most get 9 A's on screen by pressing following key sequence:
A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V
Note:
- 1 <= N <= 50
 - Answers will be in the range of 32-bit signed integer.
 
这道题给了我们四个操作,分别是打印A,全选,复制,粘贴。每个操作都算一个步骤,给了我们一个数字N,问我们N个操作最多能输出多个A。我们可以分析题目中的例子可以发现,N步最少都能打印N个A出来,因为我们可以每步都是打印A。那么能超过N的情况肯定就是使用了复制粘贴,这里由于全选和复制要占用两步,所以能增加A的个数的操作其实只有N-2步,那么我们如何确定打印几个A,剩下都是粘贴呢,其实是个trade off,A打印的太多或太少,都不会得到最大结果,所以打印A和粘贴的次数要接近,最简单的方法就是遍历所有的情况然后取最大值,打印A的次数在[1, N-3]之间,粘贴的次数为N-2-i,加上打印出的部分,就是N-1-i了,参见代码如下:
解法一:
class Solution {
public:
    int maxA(int N) {
        int res = N;
        for (int i = ; i < N - ; ++i) {
            res = max(res, maxA(i) * (N -  - i));
        }
        return res;
    }
};
这道题也可以用DP来做,我们用一个一维数组dp,其中dp[i]表示步骤总数为i时,能打印出的最多A的个数,初始化为N+1个,然后我们来想递推公式怎么求。对于dp[i]来说,求法其实跟上面的方法一样,还是要遍历所有打印A的个数,然后乘以粘贴的次数加1,用来更新dp[i],参见代码如下:
解法二:
class Solution {
public:
    int maxA(int N) {
        vector<int> dp(N + , );
        for (int i = ; i <= N; ++i) {
            dp[i] = i;
            for (int j = ; j < i - ; ++j) {
                dp[i] = max(dp[i], dp[j] * (i - j - ));
            }
        }
        return dp[N];
    }
};
这道题还有个O(1)时间复杂度的解法,好像利用了数学知识,不过博主貌似没太理解,参见这个帖子,哪位大神给博主讲解一下?
类似题目:
参考资料:
https://discuss.leetcode.com/topic/97764/o-1-time-o-1-space-c-solution-possibly-shortest-and-fastest
[LeetCode] 4 Keys Keyboard 四键的键盘的更多相关文章
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
		
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
 - [LeetCode] 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 两键的键盘
		
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
 - LeetCode 4 Keys Keyboard
		
原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...
 - Leetcode 之 Keys Keyboard
		
1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...
 - Leetcode 650.只有两个键的键盘
		
只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). Past ...
 - Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)
		
650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...
 - LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
		
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
 - [leetcode] 650. 2 Keys Keyboard (Medium)
		
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
 
随机推荐
- ASP.NET Core MVC 2.1  顶级参数验证
			
本文讨论ASP.NET Core 2.1中与ASP.NET Core MVC / Web API控制器中的模型绑定相关的功能.虽说这是一个功能,但从我的角度来看,它更像是一个错误修复! 请注意,我使用 ...
 - Python下载图片小程序
			
欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...
 - ReentrantLock 与 AQS 源码分析
			
ReentrantLock 与 AQS 源码分析 1. 基本结构 重入锁 ReetrantLock,JDK 1.5新增的类,作用与synchronized关键字相当,但比synchronized ...
 - HIVE的常用操作(HQL)语句
			
HIVE基本操作命令 创建数据库 >create database db_name; >create database if not exists db_name;//创建一个不存在的数据 ...
 - ORA-03206,当表空间不够时,如何以添加数据文件的方式扩展表空间
			
准备导入一个数据库,大约为33G,开始创建的空库表空间为自增到20G,结果自然不够,然后就开始自动扩展表空间大小 使用的如下语句 --自动扩展表空间大小 ALTER DATABASE DATAFILE ...
 - c语言字符类型作业
			
一.PTA实验作业 题目1:7-2 统计一行文本的单词个数 1. 本题PTA提交列表 2. 设计思路 1.定义整形变量i=0,count=0,flag. 2.定义数组str[999] 3.输入str[ ...
 - 201621123060 《Java程序设计》第五周学习总结
			
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承.多态.抽象类与接口 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 2. 书面作业 作 ...
 - Oracle查询用户权限
			
Oracle查询用户权限 -- 确定角色的权限select * from role_tab_privs ; 包含了授予角色的对象权限select * from role_ro ...
 - jsMath对象
			
Math对象: abs.用来求绝对值. ceil:用来向上取整. floor:用来向下取整. round:用来四舍五入取近似值. sqrt:用来开方. pow:括号内有2位参数.如pow(2,5)表示 ...
 - CentOS 7  GUI图形界面安装
			
在此之前先获取root权限,进行以下命令: 1. 在命令行下输入下面的命令来安装Gnome包: yum groupinstall "GNOME Desktop" "Gra ...