[leetcode-651-4 Keys Keyboard]
Imagine you have a special keyboard with the following keys:
Key 1: (A): Prints 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.
思路:
要想N步生成最多个A,可在N-2步的时候,Ctrl A,N-1步的时候,Ctrl C,第N步的时候Ctrl V,这样就能将N-3步生成的A的个数,翻倍。
如何确定在第几步Ctrl A,然后再Ctrl C、Ctrl V呢,需要依次判断第i-3步之前的步骤。
得到递推公式 dp[i] = max(dp[i],dp[i-j-1]);dp[i]表示第i步生成的最多的A的个数。
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];
}
[leetcode-651-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] 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 (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- 651. 4 Keys Keyboard复制粘贴获得的最大长度
[抄题]: Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on scre ...
- LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
- [LeetCode] 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解题报告—— 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 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 ...
随机推荐
- js动画之requestAnimationFrame
1.setTimeout和setInterval 在讲setTimeout和setInterval之前,先讲一下异步执行的运行机制.(同步执行也是如此,因为它可以被视为没有异步任务的异步执行.) (1 ...
- 重写equals方法(未完)
equals方法是我们日常编程中很常见的方法,Object中对这个方法的解释如下: boolean equals(Object obj) 指示其他某个对象是否与此对象“相等”. 查看该方法的底层代码如 ...
- window.location.href url含中文乱码问题
(1).页面中先对中文进行编码. 如:window.location.href = url+"&groupName=" + encodeURI(encodeURI(grou ...
- jsonp 跨域只能调用一次ajax(无法多次调用或者循环调用)
jsonp 跨域只能掉用一次ajax(无法多次调用或者循环调用) 百度搜索关键字:jsonp 只能调用一次ajax 解决方法 //回调函数设置,给后台执行 window[callback ...
- 【TCP/IP实现磁盘资源的分享-----ISCSI(互联网最小应用程序接口)】
Iscsi server: 首先把多块磁盘合并为RAID5,便于后期iscis client访问以及服务端的管理 安装 targted服务端包,以及targtedcli创建iscsi TCP/IP共享 ...
- 【ppp-chap,pap,mp,mp-group】
PPP链路端口验证(单){ PAP(明文): 主验证方: {local-user user_name:配置本地用户; password {simple||cipher}:配置验证密码; service ...
- PHP学习day1
PHP 变量规则: 变量以 $ 符号开头,其后是变量的名称 变量名称必须以字母或下划线开头 变量名称不能以数字开头 变量名称只能包含字母数字字符和下划线(A-z.0-9 以及 _) 变量名称对大小写敏 ...
- wamp2.5怎么设置虚拟域名
换了台电脑~好不顺手.老大的机器上装的是wamp.几年没用差点连怎么设置虚拟域名都忘记了.自己写点东西~做个备忘吧. 首先,版本 然后在网上百度一堆七七八八的.做的时候没那么复杂.跟phpstudy差 ...
- hive的desc命令
desc命令 desc 命令是为了展示hive表格的内在属性.例如列名,data_type,存储位置等信息.这个命令常常用在我们对hive表格观察之时,我们想要知道这个hive各个列名(基于这些具体列 ...
- IO复用——select系统调用
1.select函数 此函数用于在一段时间内,监听用户感兴趣的文件描述符上的可读.可写和异常等事件. #include<sys/select.h> int select(int nfds, ...